-1
\$\begingroup\$

So I am trying to figure out how to check the color of my sprite on collision 2d. I change my colors of sprites like this:

Color newColor = new Color(0.68f, 0.63f, 0.76f);
                    m_spriteRenderer.color = newColor;

and on the other script I have the OnCollision2d Function:

private void OnCollisionEnter2D(Collision2D col)
    {
        Destroy(col.gameObject);
    }

acording to the color I want to delete the sprite or just add points.

Thanks,

\$\endgroup\$
  • 1
    \$\begingroup\$ Why did you delete your other account? \$\endgroup\$ – Vaillancourt Oct 15 at 19:39
  • \$\begingroup\$ if(newColor==Color.red){//doSomething();} ? \$\endgroup\$ – virtouso Oct 15 at 19:40
  • \$\begingroup\$ but how can i get the new Color from a other script \$\endgroup\$ – Laurenz Oct 15 at 20:17
0
\$\begingroup\$

Use this method in your monobehavior class:

void OnCollisionEnter(Collision other)
{
    //Colors are written as 4 normalized values: (Red,Green,Blue,Alpha)
    if (other.GetComponent<SpriteRenderer>().color == (1,0,0,1)) //alt: Color.red
    {
        Destroy(gameObject);
    {
}

Let me know if that doesn't do it for some reason. This IF statement should automatically return false (do nothing) if something with a collider hits the game object this script is attached to, but doesn't have a SpriteRenderer component in the first place.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.