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?

more ▼

asked Dec 19, 2011 at 04:59 PM

avatar image

87 22 18 23

Will a static member variable of the prefab class work for you?

public class PrefabClass : MonoBehaviour { public static MyStaticClass staticStuff; public void Awake(){ if(staticStuff == null){ staticStuff = new MyStaticClass(); } } }

Dec 19, 2011 at 05:56 PM Malcolm

That seems to be a good option. Thanks!

Dec 19, 2011 at 06:27 PM AudiOishi
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 19, 2011 at 04:59 PM

Seen: 1557 times

Last Updated: Dec 19, 2011 at 06:27 PM