AManagerClass

Author: Keli Hlodversson (freyr)

Warning
'''This article is quite dated and missing a bit of explaining. Read in conjunction with Singleton.'''

Description
The following snippet shows how to add a static property called instance that will automatically find an instance of the class in the scene and return it.

This is useful for managers and other behaviours that only have one instance in the scene and need to be accessed from other classes, as it avoids having each class to keep a reference to the manager object.

Hint: A nice place to put game managers in a scene hierarchy is to create an empty game object called Managers and attach all manager behaviours to it.

Usage
Use the code example as a template when creating a manager-type script. Remember to replace all occurrences of AManager with the name of your class. To access a function, say, Foo in the manager you call it with (Where "AManager" again should be replaced with the name of your class):

Boo - AManager.boo
''I haven't tested that one btw... so please fix it if you find any errors'' -- Keli

Javascript - AManager.js
Note: Unity Javascript does not have a way to declare "static" properties (but, non-static properties is declarable), so the instance property has been replaced with a static variable that gets initialised upon start. This also means that one can not be sure that instance has been initialised if it is accessed during initialisation.