Hi all, I want to change a material in an other scene, but I do not know how to do this, I tought something like this: If you hit for example the key A in scene 1, it will change the color of an object in scene 2, and when you restart the game it will save the data.
I’m pretty new to C# and Unity too, so I hope somebody can help me.
The easiest way is to use PlayerPrefs to save what color you want the object in the seconds scene to have. Then you put a script on the object in the second scene that checks PlayerPrefs, and sets it’s own color based on that value.
Scenes are independent. They cannot directly modify each other. The common way to get around this is to use some sort of persistence, like PlayerPrefs, DontDestroyOnLoad or static to share data between scenes.
[EDIT] Looks like I misread your question. You want the data to be saved, so statics won’t help, go with playerprefs or saveSystem like @Baste and @Kiwasi suggested.
anyways, here are some of them -
If you want a save system, go with saving data using Playerprefs or using custom I/O.
If you are having trouble with save system, or if you don’t want to implement it then you can just make the variables you want to be shared between your scenes ‘static’. Basically, ‘static’ makes a variable have same value in all instances of a class. So if you have a script named ScriptA which has a variable like this -
public static int var = 10;
and if you change it’s value in one instance of class from any scene, and try to get it’s value from any other instance of the same class ScriptA, then you will always get the same value. I recommend doing more research on it, and also have a look at -
There is a difference between using save systems and statics. Save systems write the value to hardDisk as a SAVE FILE, so it can also be accessed by the game and all it’s scenes even after the game has been restarted completely. However, statics like I said are only used to share the same variable and it’s value in all instances of a class, which means the value of variable will not be saved if you restart the game.