A modular class to handle an instance of another class. Ideal for static instances of a class that also needs to be instanced more than once.




This is a class, so you use it as you’d expect with a normal custom class. The class need to be initialised via the constructor for it to function. The make the instance static you’ll just need to define your field as static and it will work like a static instance as you’d normally write it.


private static Instance<TestScript> instance;


The constructor takes a either a reference of the type or a Func to let the system reference the value for you. This can be anything like GetComponent, FindObjectOfType or any other method that returns the type you are caching.

instance = new Instance<TestScript>(this);
instance = new Instance<TestScript>(GetComponent<TestScript>);


To access the instance value, you can use the property Value or via the implicit operator.

var instanceVar = instance.Value;
TestScript instance = instance;