How can I access variable from a previous level(scene)?
2 Answers
2You will need to serialize the info and have to access it when ever you want. I don’t think you can access variables from another scene as it doesn’t really make sense as to how Unity gets its things to work.
See if this helps. Unity Serialization | Unity Blog
When you load a new scene/level, Unity normally destroys all objects of the old scene before it creates the objects of the new scene.
You can mark objects from the previous scene to survive the mass-killing when a new scene is loaded by calling DontDestroyOnLoad(…).
Be careful: If the object you mark as survivor contains references to objects from the old scene, then these references will be invalid, as the objects have been destroyed. You also have to call destroy for that object yourself when you no longer need it, as Unity will NEVER destroy that object for you.
And how I then access them?
– Mika412They are just ordinary GameObjects - so you access them like any other game object: by looking them up by name or tag. Use either 'GameObject.FindWithTag' or use 'Object.FindObjectOfType' to get your gameobject/mono-behaviour.
– Rabbit-Stew-dioThanks, it worked :)
– Mika412