Getting Script "type" across scenes

Hello. I’m setting up a global manager script that will be alive for the duration of the application. To that end, I’ve created a GameObject called ExtManager, and added an ExtManager component to it.

I’ve set the DontDestroyOnLoad property, and I’ve tested it by running the application and switching from one scene to another via application.LoadLevel(). The ExtManager gameobject remains across the scene/change change. I have a script with this code in the new scene to confirm that the gameobject remains:

GameObject obj = GameObject.Find(“/ExtManager”);
Debug.Log("extmanager is: " + (obj != null));

Where I’m running into problems is how to get a typed reference to the script component of ExtManager in the new scene. For instance:

GameObject obj = GameObject.Find(“/ExtManager”);

ExtManager comp = obj.GetComponent(ExtManager);
Debug.Log("extmanager comp is: " + (comp != null));

The error I get is the common "Expression denotes a 'type, where a ‘variable’, ‘value’, or ‘method group’ is expected.

I’m obviously missing something here. Any ideas?

Thanks.

I found the answer to one of my issues (the error concerning “Expression denotes a type…”) using this syntax:

GameObject obj = GameObject.Find(“/ExtManager”) as GameObject;
ExtManager comp = (ExtManager)obj.GetComponent(typeof(ExtManager));
Debug.Log("extmanager is: " + ((obj == null) ? " not loaded " : " loaded "));
Debug.Log("extmanager COMP is: " + ((comp == null) ? " not loaded " : " loaded "));

The explicit casting was what was tripping me up in C#. The examples for GetComponent were javascript, so I didn’t catch the extra syntax needs (fortunately another post about iPhone support had it as a passing example in C#).

BUT, that works only in the first scene where I explicitly have a ExtManager gameobject with a ExtManager script component in the scene. Once I transition via application.LoadLevel() to another scene, I can not execute the code above because of this error:

‘The type or namespace name `ExtManager’ could not be found. Are you missing…’

The newly loaded scene doesn’t have a ExtManager GameObject with a ExtManager script component like the original scene did. How can I get access to the script component as its type? Do I have to add a dummy extmanager gameobject with an extmanager script component in every scene in order for this to work? It’d be cleaner if I could just count on every scene that’s loaded being able to access the ExtManager through the above manner.

Thanks.

Yeah, GetComponent seems to trip a lot of people up when using C# or javascript with #pragma strict. It could be more explicit in the documentation, but it does specify the return type for GetComponent is always Component.

As to your other question, there are a few ways to get around it. What you suggested (having a dummy ExtManager in each scene) is the most straightforward. If you need to access members of that class, it will have to be there to access when testing, and you don’t want to have to load each scene from the beginning just to ensure it’s there.

I did something like that for a project a while ago (assuming ExtManager is intended to be a singleton). What I did was have a dummy instance in each scene, and on Awake I had each ExtManager do a check with Object.FindObjectsOfType() to get all instances of the manager class in the scene. If there was more than one, the object would destroy itself. That way, only the first loaded one would survive (since Awake is only called once during an object’s lifetime).

The other way you could do it is by making ExtManager a static class and not instantiating it at all, but if it needs to do anything in the scene (and not just exist as a resource for other classes) that’s probably not ideal.

Hey there. Thanks for the response. I appreciate it.

Both of those methods seem like a good solution, I think for my current purposes the static singleton route will probably be best, so I’ll adapt to that route. I can see other upcoming tasks that will definitely need to manipulate the scene, which makes the first idea a better choice.

Thanks again!