Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

change color with collision

Discussion in 'Editor & General Support' started by mikeheraklion, Sep 26, 2016.

  1. mikeheraklion

    mikeheraklion

    Joined:
    Sep 26, 2016
    Posts:
    2
    Hello

    I am new to unity..

    I am trying changing color of one object when there is colission.
    This is my code

    void OnCollisionEnter (Collision col)
    {

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

    GetComponent<Renderer>().materials[0].color = Color.red;
    }
    }
    }


    Thanks for help :)
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    OK, so what's wrong?

    I assume you'll need something like col.gameObject.GetComponent<Renderer>().materials[0].color = Color.red;

    Looks like you're new here, I'd recommend Roll a Ball here: https://unity3d.com/learn/tutorials

    Also this is the Unity Editor and General Discussion part of the forum. Your question should have been posted in the Scripting section of the forum. Also, please insert code with the "Insert" button when posting. Keeps things looking good :)
     
    Last edited: Sep 26, 2016