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. Dismiss Notice

Changing MATERIALS in runtime

Discussion in 'Scripting' started by Coluna, Apr 27, 2010.

  1. Coluna

    Coluna

    Joined:
    Aug 1, 2009
    Posts:
    22
    I wanna change ALL materials from a given renderer in runtime, but it seems that assigning a value to the materials array does not work. I made a simple example to show this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. class DummyMaterial:MonoBehaviour
    4. {
    5.     public Material otherMaterial;
    6.     void Start()
    7.     {
    8.         Renderer[] renderers = GetComponentsInChildren<Renderer>();
    9.         foreach (Renderer r in renderers)
    10.         {
    11.             for (int i = 0; i < r.materials.Length; i++)
    12.             {
    13.                 r.materials[i] = otherMaterial;
    14.             }
    15.         }
    16.     }
    17. }
    18.  
    19.  
    If I change the 'material' property, it works, but my meshes have more than one material.

    What is the right way to change a material?
    Thanks
     
  2. Coluna

    Coluna

    Joined:
    Aug 1, 2009
    Posts:
    22
    Just got it...you have to swap the WHOLE array, not every single element
    Code (csharp):
    1.  
    2. Material[] newMaterials = new Material[renderer.sharedMaterials.Length];
    3. ...
    4. //do something
    5. ...
    6. renderer.materials = newMaterials;
    7.  
    See ya
     
    killer_mech, malkere and idurvesh like this.
  3. Saeblundr

    Saeblundr

    Joined:
    Mar 31, 2010
    Posts:
    10
    bah, good work.
    I've been trying on and off all last weekend trying to resolve exactly that same problem.

    I do find that solution kind of counter intuitive/productive, would be nice to be able to just change 1 material... but i digress...


    thanks again!
     
  4. GravitonGames

    GravitonGames

    Joined:
    Oct 6, 2012
    Posts:
    1
    Thank you so much! I was getting crazy with this issue today. You saved my day!
     
  5. johnabraham

    johnabraham

    Joined:
    Dec 26, 2014
    Posts:
    2
    Hey guys!

    I am newbie to unity please help me out form this, i am trying to make a unlimited 2d runner game and i have 5 environments to swap as per the user selection, on the play button event i am calling the "SetTheBackground()" method out of 9 bgs only one got effected. (may be the problem is in single frame it has been called). if i kept the folowwing code in update method it's working but fps issue.

    we should not call in every frame. please help me out.

    using UnityEngine; using System.Collections;

    public class BackgroundManager : MonoBehaviour { public static BackgroundManager instance;

    1. GameObject mgameObject;
    2. publicMaterial[] bgmaterial;
    3. voidAwake()
    4. {
    5. mgameObject =this.gameObject;
    6. instance =this;
    7. }
    8. publicvoidSetTheBackground()
    9. {
    10. if(GameController.instance !=null){
    11. switch(GameController.instance._ThemeType){
    12. case eTheme.Theme1:
    13. mgameObject.renderer.material= bgmaterial [0];
    14. break;
    15. case eTheme.Theme2:
    16. mgameObject.renderer.material = bgmaterial [1];
    17. break;
    18. case eTheme.Theme3:
    19. mgameObject.renderer.material = bgmaterial [2];
    20. break;
    21. case eTheme.Theme4:
    22. mgameObject.renderer.material = bgmaterial [3];
    23. break;
    24. case eTheme.Theme5:
    25. mgameObject.renderer.material = bgmaterial [4];
    26. break;
    27. default:
    28. break;
    29. }
    30. }
    31. else{
    32. }
    33. }
    Thanks in advance!