Cross scene scripting

Can anyone tell me how to execute a script in a scene that’s about to be loaded from the Application.LoadLevel command? It can’t be a startup script in the next scene though as I need to run different scripts depending on which option the user chooses.

Cheers

Actually, it can - create a script (A) that is not destroyed on scene load to hold the users choice, then once the new scene loads, have your startup script get the user’s choice from script A.

Good plan!

I’ll give that a go. Would I drop the script into an empty game object that isnt destroyed in order to keep it?

That’s one way of doing it. Generally, you would create such a script to hold all your game’s working parameters such as sound level, score, lives, game state, etc and attach it to a game object in your splash screen.

It’s somewhat bad mojo to load that game object in an actual game level, as each time the user loads that level he’ll have another copy!

For more advanced coding implementations of the same concept, search the forums and the wiki with the keyword “singleton”

So would you have your levels loading in and out of the splash screen level? We’re not making a game as such, more a virtual walkthrough of an area that requires us to go back to the splash screen and reload a scene but starting from a new position each time. Plus my scripting skills are rubbish to say the least.

No. The reason you generally load such an object in your splash screen is precisely because the user will never see it again. You won’t end up with 10 copies of the game object. It’s a poor man’s singleton, there can be only one.

If you are not making an actual game and what you need to store between scenes is minimal, then you might want to look into playerprefs.

There’s a dozen ways to kill that cat. Choose which suits your requirements and skill set.

You can use Application.LoadLevelAdditive instead of Application.LoadLevel and remove unwanted objects manually.