Debug Problems

Updated on May 24, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on May 23, 2018

So I’m trying to make a game where when the color of the character hits a platform, it allows it to use its collider, and when it’s not the same color, the character falls through the platform/ collider. I cannot figure out how to incorporate the tags into the code and allow it to understand if its hitting the same color or not. This is what I have:

 void OnCollisionEnter2D (Collision2D col)
 {
 if (col.gameObject.tag == )
 {
 Debug.Log(col.gameObject.tag);
 }
 }

And this is what I’m using for the platform tags:

void SetRandomColor ()
 {
 int index = Random.Range(0, 4);
switch (index)
 {
 case 0:
 currentColor = "Blue";
 sr.color = colorBlue;
 break;
 case 1:
 currentColor = "Green";
 sr.color = colorGreen;
 break;
 case 2:
 currentColor = "Red";
 sr.color = colorRed;
 break;
 case 3:
 currentColor = "Purple";
 sr.color = colorPurple;
 break;
 }
}

Any help would be much appreciated.

  • Liked by
Reply
0 on May 23, 2018

So I wouldn’t use tags for this. I would on the collision check check the colors of the objects and if the color of the object the player is colliding with is the same, then you can pass through it. Otherwise you stop them where they collided at.

 

  • Liked by
Reply
Cancel
0 on May 23, 2018

Hello again Dylan!

That makes total sense and I have now tried that, but I have one more question. How can I get the collision check to check the colors of the objects? Like what would be the code for this?

  • Liked by
Reply
Cancel
1 on May 24, 2018
gameObject.GetComponent<Renderer> ().material.color
on May 24, 2018

That worked. Thanks!

Show more replies
  • Liked by
Reply
Cancel