Search Unity

Change parent material on press K c# unity.

Discussion in 'Scripting' started by MaorDany, Jun 17, 2018.

  1. MaorDany

    MaorDany

    Joined:
    Dec 31, 2017
    Posts:
    105
    I want to change material while pressing the button but it does not work for me.

    The code is on the child

    Code (CSharp):
    1. public Material Mat;
    2.     void Update () {
    3.         if (Input.GetKeyDown(KeyCode.K))
    4.         {
    5.             transform.parent.GetComponent<Renderer>().material = Mat;
    6.         }
    7.     }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    does something like this work? just for testing.
    transform.parent.GetComponent<Renderer>().material.color = Color.red;
     
  3. What exactly mean that "it doesn't work"? Do you have error messages in the console? It just does not updates the material? How do you know that? Are you sure it does not update the material?