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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Change material array

Discussion in 'Scripting' started by IbrahimAlkaber, May 7, 2020.

  1. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    Hi, I've array materials with meshrenderr array and i would like to chanage the material of car prefab but it doesn't work

    Code (CSharp):
    1. public Renderer[] BodyCar;
    2. public Material[] SelectableMaterial;
    3.   void Awake()
    4. {
    5.      ChangeDynamic();
    6. }
    7. public void ChangeDynamic()
    8. {
    9.      for(int i = 0; i < BodyCar.Length; i++)
    10.      {
    11.          BodyCar.GetComponent<MeshRenderer>().sharedMaterials[0] = SelectableMaterial;
    12.      }
    13.      Debug.Log("done");
    14. }
     
  2. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    Any help please?
     
  3. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    What about it "doesn't work"

    EDIT: BodyCar is an array but you are trying to use it like a GameObject.

    I can only guess, but perhaps this is what you want?

    Code (CSharp):
    1.      for(int i = 0; i < BodyCar.Length; i++)
    2.      {
    3.          BodyCar[i].GetComponent<MeshRenderer>().sharedMaterials[0] = SelectableMaterial[i];
    4.      }
    5.      Debug.Log("done");
     
  4. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    Hi, did not work
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    There is nothing anyone can do if you are hellbent on providing as little information as possible.
     
    edwiz7 likes this.
  6. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    I'm working on shop script and i have materials that players can change materials of car by means of buttons
    and I made an array to add all materials to the car so that it could be easier to add at any time from the component without specifying the script.