Search Unity

How to change object material more than one element in run-time?

Discussion in 'Getting Started' started by Pollawat, Apr 2, 2020.

  1. Pollawat

    Pollawat

    Joined:
    Aug 27, 2016
    Posts:
    192
    Hello sir, ma'am. I'm a newbie.

    I'm here asking for a solution to change an object material in c# script

    Here is the simple code I created:

    Code (CSharp):
    1.     public GameObject MainObject;
    2.     public Material MaterialYouNeed;
    3.     void Start()
    4.     {
    5.  
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         if (Input.GetKeyDown("i"))
    11.             {
    12.             MainObject.GetComponent<Renderer>().material = MaterialYouNeed;
    13.            
    14.         }
    15.     }

    It works fine for a simple object, but the problem is some objects have more than one material on it.




    As you can see here this object got 3, and I just don't know how to call each one of them in c#

    Thank you.