Is it possible to write any code that is NOT attached to a game object in Unity? For example:
What about the concept of a “scene” object which contains code and data specific to a scene, accessible by all game objects within that scene?
What about a “game” object which contains code and data specific to a game, accessible by all scenes within the game?
Is there a way to dynamically attach code to a game object? I’m a long time Director developer and you can do this through the “scriptInstanceList”.
If the things I mention above are not possible, what are some strategies for game and scene management? Would you attach your code to a background object or to a game object that’s always present in the scene?
Thanks,
Brian
Scripts don’t execute alone, no. You can however simply create an empty game object. It’s then an referenceable object with running code but doesn’t represent a physical structure in your game.
I do just that. I create a generic empty GameManager object that drives the scene’s state machine and encapsulates it’s relevant data. Level, Lives, Score, etc.
I do that too, with a singleton that contains generic application settings such as master volume, etc.
Yes, you can attach components at runtime.
It’s really up to the developer and what suits his tastes, Unity is very flexible in terms of giving you a lot of options in this regard. The methods I used above are mine, some may use them some may take a different approach. In the end though, Brian, everything you discussed is not only possible but quite easy to accomplish in Unity.
Everything in unity is an ‘object’ and every script you define is a class. Once you understand that paradigm everything becomes clear.
I do that too, with a singleton that contains >>generic application settings such as master >>volume, etc.
And where would you put that object? In your first scene? But isn’t it not really a singleton since the scene is destroyed after the next scene loads? Or is there a specific technique for creating a singleton? How do you persist data across scenes? Does scene 1 need to pass data to scene 2?
Yes, you can attach components at runtime.
What’s the specific call to do this?
Thanks,
Brian
Yes you can throw the object into scene 0 and use DontDestroyOnLoad to make sure it’s available in all subsequent scenes. It’s as easy as that. It’s also one of the few and rare instances where I would use static variables, as there can be only one. In the script I can, as previously referenced…
static var masterVolume = 1.0;
So then in later scenes I can reference it easily without having to grab a link to the game object.
sound.volume = GameSettings.masterVolume;
There is also an entry on the wiki and a few examples on the forums, which shows you how to enforce a singleton through code.
To add a component to an object at runtime, you simply use AddComponent!
Also, if you have static functions, the scripts they’re in don’t have to be attached to a game object; they just have to exist in your project somewhere and you can access the functions from other scripts.
–Eric
Thanks for the answers. I’m thinking in terms of a static struct or class for maintaining the game state and holding common functions.