- Home /
Yet another Object reference not set... error
First off, I've searched multiple questions, but this is such a general error that it's different for each person.
I'm trying to setup an array of GameObjects. I've done this successfully once already with
GameObject[] abs;
void Start () {
abs = GameObject.FindGameObjectsWithTag("all_buttons");
Then I call use it later with:
foreach (GameObject X in abs) {
X.light.enabled = false; }
But I want to put certain game objects in certain places in the array. Here's what I'm doing:
GameObject[] lightsequence;
void winner() {
lightsequence[0] = GameObject.Find("5Red");
That's where I get the error of Object reference not set to an instance of an object. 5Red has a tag of all_buttons; I feel like I'm putting that game object in an array when I use FindGameObjectsWithTag, but when I use just Find it's not finding it.
Thanks in advance
So I have 5 game objects that have a light component. In the foreach loop I'm turning off all the lights for all the buttons. What I'm trying to do is to turn on certain lights in a certain order based on a timer, so that's why I'm putting the objects in an array (I'll have the object in the array more than once); every time the timer "pops" it will turn off all the lights, then turn on the next light in the array (and increment the array index for the next time the timer pops).
Answer by DaveA · Oct 10, 2013 at 11:27 PM
void Start()
{
abs = new GameObject[5]; // allocate the array first, then your code follows
But, the abs code works just fine. This is in C#. I'm not at my PC that has the project so I can't test this until tomorrow. Do I have to allocate an array when I do just a Find, or because I'm trying to put objects in the array in a certain place? I.$$anonymous$$ it works for the abs because I'm letting the code to populate the array as it sees fit, but since I'm trying to put a specific object into a specific spot in the lightsequence array I need to allocate it first?