- Home /
Changing material color between scenes
Hi all, I have been working on a platformer game and was wondering something. So here's the problem, I created multiple materials for multiple things (one for player, one for foreground, one for traps, one for fire traps and so on) and when I want to change the color for example for all traps I just change color in material and it changes color for all trap objects in the scene, which works great, but the problem is, if I change color of this material in second scene, it will change colors in all scenes. Now the question is, is there any way I can solve this without creating separate materials for every scene, because that would result in excessive amount of materials? This is at the moment the only thing I can think of (except doing it with script by changing colors of material at the start of the scene, which will have to be an option if I don't find anything else).
I hope I explained it well. Thank you in advance :)
Answer by SteenPetersen · Aug 23, 2017 at 10:02 AM
Might wanna try and just change the material color depending on the loaded scene. by making some sort of materialcolor check in the heirarchy.
 public class MaterialColors : MonoBehaviour {
         public int sceneNumber;
         public Material trapMat;
         public Color color1 = Color.red;
         public Color color2 = Color.green;
         public bool materialLoaded = false;
     
         void Update(){
     
        if(!materialLoaded){
                 CheckMaterialColor(sceneNumber);
          }
 
         }
 
         public void CheckMaterialColor(int scene){
 
                  switch(scene){
                  case 1: 
                            trapMat.color = color1;
                            materialLoaded = true;
                            break;
                  case 2: 
                            trapMat.color = color2;
                            materialLoaded = true;
                            break;
                  }
         }
then just remember to set the bool materialLoaded to false when loading a scene.
I had similar thing in $$anonymous$$d, creating script that would change material parameters at the beginning of the scene, but I thought I might solve my problem without using script, but I guess not.
Anyway, thank you for your help :)
This is also a bad answer. You can simply subscribe to the change scene callback and then do it like this. Why would you do it in Update()?
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                