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. Dismiss Notice

How to save variables with a click of a button then access those variables in another scene/script?

Discussion in 'Scripting' started by radsherwin, Jul 29, 2016.

  1. radsherwin

    radsherwin

    Joined:
    Jun 22, 2013
    Posts:
    10
    This may be a confusing question. Basically I have a "main menu" with a couple buttons. Depending on the button the variables have different values. There's about 6 variables.So once you click the button it goes into the next scene. This scene has a button that lets you view those variables. I don't know how to get those variables.

    I've tried using getcomponent but that doesn't work. Then using the managerObject I used DontDestroyOnLoad() and it does save the attributes in the inspector but a couple problems come from that.
    1) this is in runtime so maybe that's a problem? Obviously in the editor I can't see the actualy values because I didn't click any buttons, but I should atleast see 0 for each variable.

    2) that script has other objects that aren't in scene2(bad programming I know) so even using "FindObjectwithtag" it just spams object deleted errors. For some reason I can't put those variables in another class and access them with getcomponent. It just doesn't work.


    Any help is appreciated hopefuly I explained my situation well.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    dealing with gameobjects that exist over multiple scenes means you need to rethink their "lifecycle".

    For a "normal" gameobject in one scene, you setup it's internal references in Awake, it's references to other gameobjects in Start and have it do things from there.

    When a gameobject is going to continue to exist after a scene change all of the external references you setup at the beginning are likely gone... you need to redo the referencing whenever a scene is loaded. This used to be done via a "OnLevelWasLoaded" function on monobehaviour (or at least I think it was called something like that) but "scene" things have moved to the SceneManager and it looks like you can do the same with

    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-activeSceneChanged.html (useful docs no? o_O try
    http://answers.unity3d.com/questions/1160006/how-to-add-a-delegate-to-this-to-get-notifications.html )
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    First of all what kind of variables are you talking about? Int? String? GameObject? Custom ones?

    1)guessing from this they are all int's. Are they public? Then you should see 0's in the inspector. If you are visualizing then with UI text you should see 0's. Not sure what you are doing here?

    2)you should split up the code into a script that only has the things you want to get to the next scene and one that has the rest. If that's not possible in your case, you can filter out parts of the script that only need to run in certain scenes with SceneManager.GetActiveScene or SceneManager.GetSceneByName and check if the code should run in this scene
     
  4. radsherwin

    radsherwin

    Joined:
    Jun 22, 2013
    Posts:
    10
    Int variables and they are all public. For your first point on seeing them as all 0s. If i'm bringing the game object over fromscene 1 I don't want to add the same script twice right?

    I got everything on different scenes! But I still can't access those variables. I made them into an array of enum's and it works. But not from another scene.
     
    Last edited: Jul 30, 2016