Accessing Variables

As you might have guessed I’m fairly new. I’m struggling to access variables I have wrote in one script, and making them accessible in another script, in another scene.

I have made the variables public static. (Not sure if that is the best way to do it) But they need to be retained across the different scenes as well. So you increase the variable in one scene, and decrease it in another. So it is essential the changes go back and forth.
I’m struggling to access the variables from my second scene, and alternative script, and alter them.

Thankyou.

Create an empty gameobject.
Add this property: Unity - Scripting API: Object.DontDestroyOnLoad
Add the static class to that gameobject.

It already is on an empty game object. "DontDestroyOnLoad’ Works beautifully, thank you. Just still can’t move those variables across.

Hi Shimmy,
There can be more than two approches , I have listed here .

  1. Using a static class in your project , which need not be attached to any scene and it can be accessed across any scripts.
  2. Make use of singleton class. or refer this link.http://wiki.unity3d.com/index.php/Toolbox

Hey. Singletons sound like exactly what I need. But I can’t get my head around them. I’ve followed what you’ve supplied but I can’t make sense of it. Any more help would be great.