Why this doesn't work ?

Error:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
Tree_System.Start () (at Assets/Scripts/Tree/Tree_System.cs:17)

using UnityEngine;
using System.Collections;

public class Tree_System : MonoBehaviour {

    public float health;

    private GameObject[] obj_tree;
    private GameObject[] obj_tree_child;

    void Start () {
  
        health = 100.0f;

        obj_tree = GameObject.FindGameObjectsWithTag("Tree");

        obj_tree[0] = GameObject.Find("Tree_Normal/Tree_1");
        obj_tree[1] = GameObject.Find("Tree_Normal/Tree_2");
        obj_tree[2] = GameObject.Find("Tree_Normal/Tree_3");
        obj_tree[3] = GameObject.Find("Tree_Normal/Tree_4");
        obj_tree[4] = GameObject.Find("Tree_Normal/Tree_Log");
        obj_tree[5] = GameObject.Find("Tree_Normal/Tree_Root");

        for (int i = 0; i < obj_tree.Length; i++) {

            obj_tree[i].tag = "Tree";

        }

        obj_tree_child = GameObject.FindGameObjectsWithTag("Tree_Child");

        obj_tree_child[0] = GameObject.Find("Tree_Normal/Tree_2/Tree");
        obj_tree_child[1] = GameObject.Find("Tree_Normal/Tree_2/Tree_Inner");

        obj_tree_child[2] = GameObject.Find("Tree_Normal/Tree_3/Tree");
        obj_tree_child[3] = GameObject.Find("Tree_Normal/Tree_3/Tree_Inner");

        obj_tree_child[4] = GameObject.Find("Tree_Normal/Tree_4/Tree");
        obj_tree_child[5] = GameObject.Find("Tree_Normal/Tree_4/Tree_Inner");

        obj_tree_child[6] = GameObject.Find("Tree_Normal/Tree_Log/Tree");
        obj_tree_child[7] = GameObject.Find("Tree_Normal/Tree_Log/Tree_Inner");

        obj_tree_child[8] = GameObject.Find("Tree_Normal/Tree_Root/Tree");
        obj_tree_child[9] = GameObject.Find("Tree_Normal/Tree_Root/Tree_Inner");

        for (int i = 0; i < obj_tree_child.Length; i++) {

            obj_tree_child[i].tag = "Tree_Child";

        }

    }

    void Update () {

        if (health <= 80) {

            obj_tree[0].SetActive(false);

        }

        if (health <= 60) {
          
            obj_tree[1].SetActive(false);
          
        }

        if (health <= 40) {
          
            obj_tree[2].SetActive(false);
          
        }

        if (health <= 20) {
          
            obj_tree[3].SetActive(false);
          
        }

        if (health <= 0) {
          

          
        }

    }
}

2337838–158053–Tree_System.cs (1.86 KB)

Index out of range means that you’re trying to access a slot in the array that doesn’t exist. In this case, it is being triggered when you try to access the first slot (obj_trees[0]), which means that there are no objects at all in the array, which means that there are no objects tagged “tree”. In order to use slots in an array you must first declare the array with enough slots. But, though that’s the main “error” issue here, I have much more valuable advice below:

One day I’m going to write a long detailed blog post entitled “Why GameObject.Find and Tags Are Never The Right Way to Do Things”, but I’d have to make a blog first so that probably won’t ever actually happen. I hate that there are so many tutorials floating around out there that tell newbies to use GameObject.Find, because it seriously, truly, is always always always a bad idea - and the alternatives to it aren’t even hard to learn. I almost wish that it didn’t even exist in the engine at all - it’s that useless. I think maybe once in my entire professional life has GameObject.Find been the right way to go about things. Tags are slightly less useless, but only because they perform better than some of their alternatives - I’m not going to get into tags here though.

There are several different methods for getting references to GameObjects. The most basic is the public reference, and that’s exactly what I’d recommend in this case. In your case, simply make obj_tree and obj_tree_child public instead of private. Then you can drag in the objects you want, rather than hard-coding their names into your script file. And yes - it’ll fix this problem, because you’ll be able to see and change the size of the array.


Speaking of removing hard-coded stuff, I can see where you’re going in that Update function, and I have a better recommendation.

public int healthPerTree = 20;
void Update() {
for (int t=0;t<obj_tree.Length;t++) {
int maxHealth = 80 - t * healthPerTree;
obj_tree[t].SetActive( health <= maxHealth );
}
}

It’s compact, it’s customizable, it’s extensible, and it guarantees no index-out-of-range errors.

1 Like

Hello sir, first thing you should read posts giving advice as to how to ask for help on a forum.

When you’ve done this you should stop using the GameObject.Find() familly until you have several years of experience and know very well why and when not to use it.

Work on your variable naming convention, and use the Unity’s editor feature called the Inspector. Make your fields either public or use the [SerializeField] attribute if you’re that kind of person and drag & drop elements of interest of your hierarchy in it.

For the rest i’ll let you read what @StarManta wrote while i was writting this ^^.

2 Likes