Rf Mvs
Rf Mvs

Reputation: 166

How can detect when 2 objects collision with the same color?

I have 1 problem with Unity:

I have 1 object with a collision with 2 objects, the first is blue and the second is red.

I would like to know how detect when both objects are the same color and do determinate action Collisin between 2 objects is clear but how to detect your color is so difficult for me.

¿ how can do it?

colision:

public class Colision : MonoBehaviour {

    //public GameObject HaloPrefab; // empty with halo applied to it...

    public Text points;


    void OnCollisionEnter(Collision col){


        if ( col.gameObject.name == "Cube") {

            col.gameObject.SetActive(false); // Lo que hago es que si colisiona desaparezca el objeto, pero necesito que haga eso si ambos son del mismo color. 
        }




        if ( col.gameObject.name == "Cube(Clone)") {

            col.gameObject.SetActive(false);


        }     
}

my object can change color and the code is this: and works

public class ChangeColor : MonoBehaviour {

    public Material[] materials;
    public Renderer rend;

    private int index = 1;

    // Use this for initialization
    void Start () {

        rend = GetComponent<Renderer> ();
        rend.enabled = true;

    }

    public void Update() {
        if (materials.Length == 0) {
            return;
        }
        if (Input.GetMouseButtonDown (0)) {
            index += 1;

            if (index == materials.Length + 1) {
                index = 1; 
            }
            print (index);

            rend.sharedMaterial = materials [index - 1];                        
        }
    }
}

Upvotes: 0

Views: 835

Answers (1)

Umair M
Umair M

Reputation: 10720

Something like this:

void OnCollisionEnter(Collision col)
{
    var me = gameObject.GetComponent<Renderer>();
    var other = col.gameObject.GetComponent<Renderer>();
    if (me != null && other != null)
    {
        if (me.sharedMaterial.color == other.sharedMaterial.color)
        {
            // congratulation you are colliding with same color.
        }
    }
}

Upvotes: 2

Related Questions