Search Unity

I can't change the material

Discussion in 'Scripting' started by LumoKvin, Nov 15, 2019.

  1. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    This is probably something simple but I can't figure it out. I am trying to change the material but it isn't working. What I am doing wrong?

    Code:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         mat = GetComponent<Renderer>().material;
    4.         Debug.Log("Start()" + " - mat: " + mat.ToString() + " - newMaterial: " + newMaterial.ToString());
    5.     }
    6.    
    7.     public void changeMaterial()
    8.     {
    9.         mat = newMaterial;
    10.         Debug.Log("changeMaterial()" + " - mat: " + mat.ToString() + " - newMaterial: " + newMaterial.ToString());
    11.     }
    Log:
    Code (CSharp):
    1. Start()
    2. - mat: Black (Instance) (UnityEngine.Material)
    3. - newMaterial: Crystal_Blue_Mat (UnityEngine.Material)
    Log:
    Code (CSharp):
    1. changeMaterial()
    2. - mat: Crystal_Blue_Mat (UnityEngine.Material)
    3. - newMaterial: Crystal_Blue_Mat (UnityEngine.Material)
     
    Last edited: Nov 15, 2019
  2. Deleted User

    Deleted User

    Guest

    newMaterial is public, did you fill the property in the Inspector?
     
  3. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    Yes. I did fill that property.
    Capture.PNG
     
  4. Deleted User

    Deleted User

    Guest

    Where are you calling ChangeMaterial()?
     
  5. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    The script is attached to the object that I want to change the material. I am calling it when the player enters a trigger (box collider).

    As you can see from the debug log, the method is being accessed and the material values seem correct.
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    You're not actually updating the renderer's material.
    instead of this:
    Code (csharp):
    1.  
    2.   void Start()
    3.     {
    4.         mat = GetComponent<Renderer>().material;
    5.     }
    6.    
    7.     public void changeMaterial()
    8.     {
    9.         mat = newMaterial;
    10.     }
    11.  
    do this:

    Code (csharp):
    1.  
    2. Renderer myRenderer
    3.   void Start()
    4.     {
    5.         myRenderer = GetComponent<Renderer>();
    6.     }
    7.    
    8.     public void changeMaterial()
    9.     {
    10.         myRenderer.material = newMaterial;
    11.     }
    12.  
     
  7. Deleted User

    Deleted User

    Guest

    This it is. I was about to write this, why did I hesitate? Oh well...

    @kdgalla Anyway, shouldn't this be considered a bug?
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Why?
     
  9. Deleted User

    Deleted User

    Guest

    We can access the material with:
    Code (CSharp):
    1. mat = GetComponent<Renderer>().material;
    but we cannot act on it.

    To act on it, we must use:
    Code (CSharp):
    1. mat = GetComponent<Renderer>();
    2. mat.material = newMaterial;
     
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I think you misunderstand how C# reference types work.
    (Edit: actually the logic is the same whether it's a reference type or not.)
    in this case:

    mat = GetComponent<Renderer>().material;


    mat is a reference variable pointing to the Renderer's material. using mat, LumoKvin could have set any of the properties (i.e. color, mainTexture whatever) and it would have updated the renderer, because mat and GetComponent<Renderer>().material are both referencing the exact same material.

    That's not what LumoKvin did, though. Instead, he assigned mat to reference a different material, by typing
    mat = newMaterial;

    At that point, mat is no longer referencing the renderer's material, it is referencing a completely different material that has nothing to do with the render. So once you assign mat to a different material, it would not make any sense for it to update the render's material.
     
    Last edited: Nov 15, 2019
  11. LumoKvin

    LumoKvin

    Joined:
    Sep 28, 2019
    Posts:
    195
    I works. Thanks!
     
  12. Deleted User

    Deleted User

    Guest

    It's still quite confusing, especially if you don't do that every day. :)