- Home /
making variables work in two scenes
Hey!
I'm trying to make a game where you can affect how the world(scene1) looks like in scene2.
in scene2, I got booleans that I want to be able to affect objects in scene1.
I've tried using "DontDestroyOnLoad", but I think the problem lies in that I have to have the object with the booleans in both scenes. Is there another way to make the variables reach the objects in scene1 or did I do something wrong with "DontDestroyOnLoad"?
here's the code for the object with booleans:
using UnityEngine;
using System.Collections;
public class idea : MonoBehaviour {
public bool shadow1 = false;
public bool shadow2 = false;
public bool shadow3 = false;
void awake () {
DontDestroyOnLoad(transform.gameObject);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Answer by loopyllama · Oct 11, 2011 at 10:01 AM
make script SceneTwo with a static bool myBool and in SceneOne do SceneTwo.myBool = true;
static variables do not belong to instances of a class, they belong to the class. if you had a script that had a public static variable, you can set/get that variable from either scene without it being on an object. so in your case you can make a new script with a public static bool and access it like this from scene 1 and scene 2: $$anonymous$$yScript.myBool = true or bool foo = $$anonymous$$yScript.myBool where $$anonymous$$yScript is not applied to any object.
Your answer
Follow this Question
Related Questions
How do I compare boolean variables? 1 Answer
Some Boolean "Action" Trouble 2 Answers
Why does monobehavior in if statement compiles? 1 Answer
How to make the level screen????? 0 Answers
how to create boolean Varibles ? 3 Answers