- Home /
Accessing non-static functions in an unattached, non-scene, "project" script
Hello,
Today I've tried to use LoadLevelAsync for the first time. I have some comic-book images going-on at the screen during the loading process. My idea was to start async download as soon as the comics section starts, and then, when the next level is loaded, I wanted to display a "skip" button that will allow the player to skip the comics and start the loaded level.
I had the GUI function that drives the comics, and the coroutine that loads the level in the same script attached to the object in my scene. I thought that all I need to do is start loading the level and then yield, and then change my static variable levelLoaded to true.
In OnGUI I had it to draw a "skip" button if levelLoaded. But, it doesn't work. I guess it doesn't work because my object with that script is in the scene and it gets destroyed before the levelLoaded variable becomes true. It gets destroyed when the next level is loaded and then it gets recreated. levelLoaded var never becomes true.
So I thought I should make a script with my LoadLevel coroutine and keep it in "project", not to attach it to any object in my scene so it doesn't get destroyed-recreated (restarted). I tried this using "static function" but it can't work since yield can't be used in static functions.
I can think of many ways to go around this problem, but I'm curious now. Is there a way to access non-static functions in "free", unattached scripts that are not in the scene? Javascript please.
Thank you even for reading this :)
I know that I can make this object not to get destroyed on level load, that's one of the ways to go. But I'm still curious about accessing coroutines in "free" scripts.
Answer by Itinerant · Dec 05, 2012 at 09:29 PM
I don't think this is possible, at least not without stepping outside of our normal javascript/C# environment. Anything you use has to be loaded, and once it's loaded it's in the scene. Uber has a really good idea, though, with using a object with a dontdestroyonload command. You can learn the details of how to use it at http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
Only exception I can find to this is by using static variables, which you mention you don't want to use: http://answers.unity3d.com/questions/33229/how-to-pass-boolean-variable-into-another-scene-wi.html