Checking color to enable/disable collider

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

So in my 2D game I have a code that randomly generates a color for my character. What I want to do is make a new code that checks for the color of the character and either enables or disables the collider for the different colored platforms I have (The blue character will jump on the blue platform but not the red one). My problem is that when I put in the code to disable the platform collider, it stays disabled, making it to where the platform collider is disabled indefinitely (The red platform collider will not let the red character jump on it). Is there a way one of you fine people could fix the code for me?

void OnCollisionEnter2D(Collision2D col)
 if (gameObject.GetComponent<SpriteRenderer>().color != this.gameObject.GetComponent<SpriteRenderer>().color)
 RedSquare.GetComponent<Collider2D>().enabled = false;

Any help or pointers would help a ton. Thanks!

  • Liked by
4 on May 24, 2018

gameObject and this.gameObject are the same gameObject

also, you only ever disable RedSquare‘s collider. You never even access the object you collided with.


on May 24, 2018

Ok so I could make the first gameObject player and second gameObject RedSqaure. Correct? Then how could I fix the second problem?

on May 24, 2018

You just need to re-enable the collider at some point.

on May 24, 2018

Are you familiar with a code that may reset the collider on the press of my reset button?

on May 25, 2018

RedSquare.GetComponent<Collider2D>().enabled = true;

Show more replies
  • Liked by