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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing data between scenes

Discussion in 'Scripting' started by Barft, Aug 6, 2015.

  1. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    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!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    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.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  4. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Yes about DontDestroyOnLoad, can you tell me more about it, how can I use it?
     
  5. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Hi thanks for your reaction, I thought that too, but I do not know how to write it, can you help me with it?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  7. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
  8. Batman_831

    Batman_831

    Joined:
    Oct 17, 2014
    Posts:
    106
    [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 @BoredMormon 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 -
    Code (CSharp):
    1. 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.
     
    ameriknho1991 likes this.