- Home /
What the best way to initialize constant static data in a MonoBehavior?
I have a prefab that get instantiated many times when my scene loads.
The prefab has a MonoBehavior script attached that initialized a list of C# delegates and also a list of co-routines. Functions/methods will be chosen at random, and called on the prefab instances.
These lists need to be initialized only once, and they will be identical for each instance of the prefab.
Whats the best way to initialize the lists once and have each instance of the prefab refer to the lists?
Will a static member variable of the prefab class work for you?
public class PrefabClass : $$anonymous$$onoBehaviour { public static $$anonymous$$yStaticClass staticStuff; public void Awake(){ if(staticStuff == null){ staticStuff = new $$anonymous$$yStaticClass(); } } }
Your answer
Follow this Question
Related Questions
How can I instantiate a prefab from a static function? 1 Answer
class constructor/order/initialization confusion 1 Answer
Is it possible to control the order of a static's initialization? 2 Answers
Singleton instance accessed in coroutine always null 1 Answer
Static Coroutine being called endlessly 2 Answers