0
votes

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?

2
I guess you have 2 instances of Constants at the end because LoadLevel("start"); will create an instance again.Kay
I have a script I call "the mule" that basically does this. gist.github.com/3919639Calvin

2 Answers

2
votes

You should have a "bootstrap" scene with all things that won't be destroyed. It would be the very first scene. It would call your "start" (that wouldn't have the Constants) and you would always load "start" from the subsequent scenes, that is, no new Constants would be created since "bootstrap" is always called once, in the beginning.

0
votes

Another solution is creating a static class; here is a link to the same question on answers.unity: http://answers.unity3d.com/questions/334959/not-passing-new-values-to-object-that-has-not-been.html