Not able to retrieve Array of Game objects

HI,

here is have 2 scripts, the GameManager in which on start instantiates all game objects of the scene.
i have these GameObjects Connected to public functions which can be accesed from another script. except that when i call for the function get ball, i do not retreive the objects at all.

some help would be appreciated :slight_smile:

in the CueInstantiator Script i have the lines of code:

for(int i = 0; i < ball.Length; i++){
ball[i]  =  GAME_MANAGER.getBall(i).GetComponent<Rigidbody>();
}

this code is returning nothing…
GAME_MANAGER is the GameObject which holds the Script GameManager…

One thing that jumps out at me is that GameManager is initializing the balls in Start(), but CueInstantiator is trying to access them from its own Start() method. Unless you’ve specifically modified the call order, there’s no guarantee which one of those Start() methods is going to run first, so CueInstantiator may be trying to get the balls before GameManager has instantiated them.

The general rule-of-thumb is that objects should initialize themselves in Awake(), and then try to talk to other objects in Start(). All Awake() functions run before all Start() functions (within a frame), so this is an easy way to ensure that internal initialization happens first.

If you can’t do that for some reason, you can play with the Script Execution Order, or you can modify your APIs so that all public functions check for initialization before doing their thing.

1 Like

i got a similar issue of this sort and my gameobject array with attached spriterenderer coming to NULL, this happens when i declare game object as gameobject[ ] balls;

i solved this issue by using list instead and it just worked then… like
List balls;

then just do balls.add(instantiate(…));

1 Like

i did the list and it worked… also there was another issue in which the script i was referring to was a prefab and not in the hierarchy. i fixed the issue and made the instantiations on awake() and it worked great… Thanks…