- Home /
How can I return a GameObject?
Hi, I'm trying to create a Singleton Class that have all the GameObjects of the game. This class is responsable to provide to all classes the GameObject. It have a function that, given a index, return a GameObject. However, when I make a instance of this singleton and request a GameObject, this function return a NULL value.
Can I make: GameObject objectTable= bank.GetObject(0);
?
objectTable = GameObject;
bank = Singleton Class;
GetObject(0) = Function of the Singleton Class that recive a index as parameter and return a corresponding GameObject;
Yeah, and how does that function work? You apparently don't have any GameObjects with index 0, but without any code or structure we can't really help you
Answer by unity_zBlSEjXgI3rPiQ · Sep 26, 2018 at 08:09 AM
I don't know how your code work. Here is an example for simple singleton implementation
public class SingletonExample{
public static SingletonExample instance;
void Awake(){
instance = this;
}
public GameObject GetObject(int index){
//Dosomething
}
}
Implement
SingletonExample singleton = SingletonExample.instance;
GameObject object = SingletonExample.instance.GetObject(0);
Hope this could help you