Game Development Reference
In-Depth Information
set; }
//public property for manager
public string MyTestProperty = "Hello World";
void Awake()
{
//First we check if there are any other instances
conflicting
if (Instance != null && Instance != this)
{
//Destroy other instances if they are not the same
Destroy(gameObject);
}
//Save our current singleton instance
Instance = this;
//Make sure that the instance is not destroyed
//between scenes (this is optional)
DontDestroyOnLoad(gameObject);
}
//public method for manager
public void DoSomethingAwesome()
{ }
}
There are more complicated setups for singletons. If you so wish, you can read them at
ht-
Note
There is another pattern named
Dependency Injection
. A more robust way to handle the
need of manager- or factory-type requirements in any project is to implement an
Inver-
sion of Control
(
IoC
) pattern, such as Dependency Injection.
Dependency Injection is a large subject, so we won't cover it in this topic. The goal here is
to make you aware of all the options when architecting your project. If you would like