x


not passing new values to object that has not been destroyed

i have 2 scenes and wish to pass info from one to another. I have created in the first scene a script Constants on an empty game object that retains the data that i wish to save for the next scene. Here it is:

 public class Constants : MonoBehaviour {
     
     public string scratchImageFront;
     public string scratchImageBack;
 
     void setScratchFront(string _scratchFront)
     {
         scratchImageFront = _scratchFront;
     }
     
     void setScratchBack(string _scratchBack)
     {        
         scratchImageBack = _scratchBack;
     }
     
 } 

before moving to the next scene, i call to not destroy the Constants script:

             GameObject constants = GameObject.Find("Constants");
             Constants script;
             script = constants.transform.GetComponentInChildren<Constants>();
                         
             DontDestroyOnLoad(constants);
             Application.LoadLevel("scratch");

I then collect the data in my second scene and all is good. after i'm done in my second scene, i go back to my first scene

 Application.LoadLevel("start");

and redo the same steps as i first did, the Constants script doesn't retain the new strings passed to it, it retains the old ones. Why? i need to destroy the Constants script in my second scene so that everything works as it should, but i don't want this. What am i doing wrong?

more ▼

asked Oct 19, 2012 at 12:55 PM

badescuga gravatar image

badescuga
11 19 24 36

I know someone is going to disapprove of this idea I personally hate the scene changing experience because of this problem. And I hated that there wasn't a direct way to save data after you end runtime

So I solved both problems by saving all relevant data values to a text file which you can look up how to do in C# with filestream I believe and to get it to work with unity u have to look up Application.dataPath

Oct 19, 2012 at 02:07 PM lil_billy
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

A simple way that is ok here is to use a static class.

 static class DataToStore{
    var data1:int;
    var data2:int;
    var wava:string;
 }

Put this in a script, no need to give the script to any object. In your game, just call

 DataToStore.data1 = theDataToStore;

A static class is made of static variables that are created before the game starts and are destroyed after the game is over (or crashes). You can then access your data anywhere and anytime in your game. It can be convenient to have a storage class for info that you want to keep all over the game like score, achievements and so on.

more ▼

answered Oct 19, 2012 at 02:25 PM

fafase gravatar image

fafase
24.6k 63 63 114

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1978

asked: Oct 19, 2012 at 12:55 PM

Seen: 514 times

Last Updated: Oct 19, 2012 at 02:25 PM