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 () {

}

}

1 Answer

1

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: MyScript.myBool = true or bool foo = MyScript.myBool where MyScript is not applied to any object.