- Home /
Storing component of instance in an array
I'm trying to store the scripts from several different instances of the base class "AeroplaneControls" within an array. So that I can later use this array to quicker access all the different methods contained in the subclasses. However when I do this I get the following error "Object reference not set to an instance of an object"
GameObject[] Vehicles;
AeroplaneControls[] Controls;
Vehicles = GameObject.FindGameObjectsWithTag ("Vehicle");
Controls = new AeroplaneControls[Vehicles.Length];
for(int i = 0; i < Vehicles.Length; i++){
Controls[i] = Vehicles[i].GetComponent<AeroplaneControls> ();
}
It is not refeering to my gameObject "Vehicles" since I can do other stuff with it like find it's position, so the problem is with my "AeroplaneControls" script array.
There is no mention of an "enemy" variable anywhere in your code.
Check if all elements of Vehicles actually have the AeroplaneControls component. Use Debug.Log lines to print the result to console.
It was a typo, I meant "Vehicles". And yes, every Vehicle has a component which is a subclass of "AeroplaneControls" (AeroplaneControls is and abstract class).
I did try debug.log before the last part: Controls[i] = Vehicles[i].GetComponent ();
And my result was: For the gameobject
Aeroplane2 (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
And for the script:
Aeroplane2 (AeroplaneScript2)
UnityEngine.Debug:Log(Object)
So it's refeering to the correct gameobject, and to the same gameobjects script that i want to access. So i don't understand why the error occurs when both are correct.
Don't know if it makes any differance, but it worked before putting the scripts into an array. And now when I go back to doing without array it doesn't work anymore.
Answer by daniel-eherbert · Nov 07, 2016 at 02:59 AM
Edit: brain fart...
Your loop is likely failing after it processes all the items due to an incorrect loop limit.
This code:
for(int i = 0; i < Vehicles.Length; i++) {
Controls[i] = Vehicles[i].GetComponent<AeroplaneControls> ();
}
...should probably be (note the
- 1
):
for(int i = 0; i < Vehicles.Length - 1; i++) {
Controls[i] = Vehicles[i].GetComponent<AeroplaneControls> ();
}
Array indices start with
0
, so if you iterate while
i < Vehicles.Length
the loop will run 1 time too many (0, 1, 2, 3, ... 20 = 21 iterations)
That is unfortunately not the case. i < 5 for example will iterate 4 times, since the 5th time it's equal and not less than 4. And since there in this example 5 items in the list I get the positions (0,1, 2, 3, 4), which are every one I need. Furthermore if that was the issue then I would get an error saying "array index out of range" since nothing exists in position 5.
$$anonymous$$y problem is most likely with the component itself, but I have no clue since whatever I do now it still doesn't work, even though it worked before
You're absolutely correct! Quite the slip of $$anonymous$$d there - apologies for that.
Are you able to use a debugger and step through the loop to see which part of it is tripping it up?
I see in the comment above that you've tried debug.log and appear to be getting values for each iteration...
Are you able to share more of the script?
What line exactly does the error occur on?
Your answer
Follow this Question
Related Questions
Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer
Access water4 scripts via my script? 1 Answer
Calling an Audio Source on one game object from a script on another game object..? 1 Answer
Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer