Search Unity

Change a material color in C# Unity 5.

Discussion in 'Scripting' started by Oblix, Jun 19, 2015.

  1. Oblix

    Oblix

    Joined:
    Dec 4, 2014
    Posts:
    7
    Hi guys , I've got a problem i've been having all day and I'd really just like to get it sorted :(.
    I'll try my best to illustrate , I've maxed out what google can tell me.
    Within Unity 5 :
    I want to change the color of a material , to the color of another material ,or at random between a list of colors.
    I've got a dancefloor that needs to change colors every time a beat is picked up. So far i've got it set up to simply swap between two materials however I want the color of the first material to be different each time the beat is detected.

    Whats the best way to set up a materials color change ? Its just a standard material. So far I've tried

    objeto.GetComponent<Renderer>().material.color =("_TintColor",( 0.3,0.3,0.7);

    Which is no good. Along with
    objeto.GetComponent<Renderer>().material.color = Color(Random.Range(0.0,1.0),Random.Range(0.0,1.0),Random.Range(0.0,1.0));

    I'm not a great scripter I really just edit values in scripts I pick up off the web but i've gone through about 5 different versions today and can't seem to simply get a materials color to change.

    Can supply pictures if needed.
     
  2. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37
    There are a lot of ways to do that... (I have not actual tested this code)
    What I do are to make a array of the materials to use. If you just change the material on a Renderer with by for example, changing the
    Code (csharp):
    1. objeto.GetComponent<Renderer>().material.color = ...
    then you are actual creating a new material each time, and the garbage collection will be a bit busy.

    What I do are to first make a array of the materials and colour I'll be using.
    Code (csharp):
    1. Material[] myMaterials = new Material[8];
    and then assigning materials to the array.
    Code (csharp):
    1. myMaterials[i] = objeto.GetComponent<Renderer>().material;
    2.   myMaterials[i].color = MakeMyColour(i);  // for some reason this makes new materials (no new statement)
    then i assign it to the gameobject when needed
    Code (csharp):
    1. someObject.GetComponent<Renderer>().material = myMaterials[......];
    Another thing that if you do:
    Code (csharp):
    1. myMaterials[nn].color = MakeMyColour(j);
    Then you will change all objects with that material at the same time.

    P.S. ( you probably know it already...)
    And if possible, avoid
    Code (csharp):
    1. objeto.GetComponent<Renderer>()
    in update() on or the other dosent matter much, but to many will slow things down try to use
    Code (csharp):
    1. private Renderer objetoRenderer;
    2.   void Start () {
    3.     objetoRenderer = objeto.GetComponent<Renderer>();
    4.     .....
    5.   }
    6.   void Update () {
    7.     ....
    8.     objetoRenderer.material = myMaterials[......];
    9.     ....
    10.   }
    D.S.