Project/Global Variables

I’m trying to figure out a way to pass variables from one scene to another. Anyone know the best way to do this?

Store them in a script attached to an object which contains the DontDestroyOnLoad function. At the beginning of every scene, all objects are destroyed from the previous scene, so an object with a script which contains that function is saved from that destruction.

You might also want to consider using a singleton for easy access to your global data.

Hello,

I’m following the tutorial on how to make a singleton on the unity wiki. Though it makes sense, I just can’t seem to get it to work.

I’m trying to do the non-component based score singleton.

public class MySingleton

{
    private static MySingleton instance;
   
    public MySingleton()
    {
        if( instance != null )
        {
            Debug.LogError( "Cannot have two instances of singleton. Self destruction in 3..." );
            return;
        }
       
        instance = this;
    }
   
    public static MySingleton Instance
    {
        get
        {
            if( instance == null )
            {
                new MySingleton();
            }
           
            return instance;
        }
    }
}

then Calling it in my code like so:

MySingleton.Instance;

but I keep getting the error:

BCE0005: Unknown identifier: 'MySingleton'

Is it not possible to script static objects that don’t inherit from MonoObjects that need to be attached to GameObjects? This method of coding game info seems a little alien to me, but if it’s the only way to do it then I guess that’s how I have to do it.

Any help would be appreciated.

I could be wrong, so somebody please correct me if I am, but, either you need to make an instance of the singleton like so:

mySingletonObj = new MySingleton();

or you need to declare the class as static like so:

public static class MySingleton{
// ... code here
}

Somebody more experienced with singletons will probably know which (if either) is better to use :slight_smile:

Hope this helps,
Nathan

Could it be that your singleton script is C#, and the script that is trying to access it is Javascript? Because then, that’s the problem. C# and JS are compiled at different times, which means that when your JS is compiled “MySingleton” effectively doesn’t mean anything to the compiler.

The easiest way to solve it would be to just move the C# script into Standard Assets (that’ll make it compile first), but I think mixing C# and JS is making things more complicated than they have to be for a simple thing like this. I’d just go with static variables in a standard JS class, or reimplement the singleton in JS like so:

class MySingleton {

	static var instance : MySingleton;

	private function MySingleton() {
	}

	static function GetInstance() {
		if( instance == null ) {
			instance = new MySingleton();
		}
		return instance; 
	}
}

Oh, and Nathan, it’s neither. :wink: The code already instantiates the singleton when you access it, and you can’t make the whole class static if you want it to have instance members, which you want if you’re using the singleton pattern.

Thanks for clearing that up tolm :slight_smile:

Thanks tolm,

that’s exactly what I was doing. I’m going to try making those changes. Thanks for the reply.