- Home /
Problem when using script in multiple scenes.
Not really a question, just something I noticed that might or might not be a bug.
I have two scenes "Level1" and "Tutorial". Both scenes have an object called "Loader" (not a prefab, just two different objects in two different scenes). Both loader objects share one script (Loader.cs). This script has public variables that are assigned in the editor.
For some reason both scenes use the values assigned in the scene "Level1", (so values assigned in the scene "Tutorial" are ignored).
Is this a bug or is it by design?
Edit: The code of the Loader.cs. If I set test to 5 in "Tutorial" scene and 10 in "Level1" both scenes print out Test in this scene is: 10
using UnityEngine;
using System.Collections;
public class Loader : MonoBehaviour {
public int test;
public Player player;
public Canvas hudCanvas;
public GameObject connectingPanelPrefab;
[HideInInspector]public static bool isEndOfTurn = false; //is set to true when player ends turn and actions are beeing processed/displayed
public GameObject AIPrefab;
//Maps
public GameObject map1Prefab;
public GameObject tutorialMapPrefab;
void Awake () {
Debug.Log ("Test in this scene is: " + test);
//create the connecting panel
GameObject connectingPanel = Instantiate (connectingPanelPrefab) as GameObject;
connectingPanel.transform.SetParent (hudCanvas.transform, false);
//set up single player game
if ((GameData.currentMode == GameMode.SinglePlayer)) {
Instantiate (map1Prefab);
BattleManager.CreateSinglePlayerGame ();
}
//set up tutorial
else if (GameData.currentMode == GameMode.Tutorial) {
Instantiate (tutorialMapPrefab);
BattleManager.CreateSinglePlayerGame ();
}
}
}
maybe it's the same object? if you change it in level 1 will this reflect in tutorial?
I tried changing the value of test
to 11 in "Level 1" during runtime, then I quit to "$$anonymous$$ain$$anonymous$$enu" scene, and then I started the tutorial. The value of test
when starting the tutorial was still 10.
this shouldn't happen in any way but we can't see the code so it's really hard to give an answer. if you could post some code would help us.
I added the code, but there really isn't anything interesting about it.
recommend some sanity checks. alter debug to confirm different objects:
Debug.Log ("Test in this scene is: " + test + "for object id:" + this.GetInstanceID() + " name:" + this.name);
just to ensure its not an issue with when awake is called..try a debug also in OnEnable()
I just tried that and this.name
are both "Loader" (that's kind of expected since both objects have the same name) but this.GetInstanceID()
returns different value in each scene.
$$anonymous$$oving Debug.Log()
to OnEnable()
didn't change anything.
I suspect it might be somehow caused by the fact that I duplicated "Level1" scene to make "Tutorial" scene.
Can you assure that nothing else alters the "test" variable? It is public, after all.
$$anonymous$$aybe run a test making it private (and using [SerializeField] ofc, so you can set the value in the inspector)?
I tried what you suggested, but the result is still the same, test
is 10 in both scenes.
Answer by Masterio · Mar 11, 2017 at 09:08 PM
On scene load the object is loaded from current scene so you can do 2 things:
Make static variables or make one object shared between scenes you can use this:
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Yes, but the point is I don't want to share this variable between two scenes. I want it to have 2 different values in two different objects (which is imo the expected behavior). but for some awkward reason it's not working this way.
Btw there is no direct scene transition between those two scenes. Scenes go in order "$$anonymous$$ain$$anonymous$$enu" -> "Tutorial" -> "$$anonymous$$ain$$anonymous$$enu" -> "Level1"
Your answer
![](https://koobas.hobune.stream/wayback/20220612112920im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
When would I encapsulate variables? 2 Answers
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers
Evaluating animation curves in the Timeline 1 Answer
Seperating editor and runtime data with ScriptableObjects 1 Answer
I can only select a prefab as my GameObject in the editor. 1 Answer