- Home /
Having a static GameObject pointing to a prefab?
Basicly ive got the same problem as posted [here][1]:
But i dont understand the answers there, so im gonna post my own question here:
I want a static GameObject variable which points to a prefab.
Sounds pretty simple right?
I would love the variable to be private, and only accessible to other scripts by a peroptery with only a get-accessor.
Someone please help :<
private GameObject mPrefab = Resources.Load("$$anonymous$$yPrefab");
public static getPrefab { get { return mPrefab; } }
Is this the only-real solution?
Answer by Berenger · Feb 06, 2012 at 12:53 AM
You don't really want a static variable here, a variable accessible without an instance. You want a GameObject referencing a prefab, and being able to access it from everywhere.
So, create a script with a public variable GameObject, attach to an empty GameObject in your scene and affect the prefab to the variable. Change the tag of the object to something fancy. Step 2, add a static function to that script which return a gameObject :
public static GameObject GetPrefab(){
return (GameObject.FindGameObjectWithTag("MyFancyTag").GetComponent<MyScript>() as MyScript).prefab;
}