Changing data between scenes

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.

Thanks all!

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.

1 Like

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.

Yes about DontDestroyOnLoad, can you tell me more about it, how can I use it?

Hi thanks for your reaction, I thought that too, but I do not know how to write it, can you help me with it?

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

Alright, thanks!

[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 -

  1. If you want a save system, go with saving data using Playerprefs or using custom I/O.

  2. 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 -

Official Unity Tutorial On Statics.
**-**Quick, short, simple and effective. Highly Recommended.

One important thing to note -

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.

1 Like