0

I'm trying to access a List from another script. It gives me the error in the title. What did I do wrong?

TreeGen.cs

 public GameObject Instree;
 int trees;
 public List<int> TreeCount;

void Start() {

trees = Random.Range(100, 300);

     for (int i = 0; i < trees; i++){

       {
            Instree = Instantiate(tree, position, Quaternion.identity);
            TreeCount.Add(Instree.GetInstanceID());
       }

}

TreeDestroy.cs

TreeGen treeGen;

void Update(){

for (int i = 0; i < treeGen.TreeCount.Count; i++) {
            {
                if (treeGen.Instree.GetInstanceID() != treeGen.TreeCount[i])
                {

                    //something to do

                }
            }
        }

}

Note: I know this shouldn't be in Update(), but I want to know if it even works

Abyzls
  • 33
  • 6
  • Do you not get or set the instance of TreeGen anywhere? Then thats your problem. – Daniel M Sep 11 '21 at 18:00
  • I don't see where you initialize `TreeGen treeGen` ... Besides that I don't quite get your logic ... the `Instree` will always be the last tree added ... So instead of a loop you can as well just say `var i = treeGen.TreeCount.Count - 1;` ... – derHugo Sep 11 '21 at 18:00

0 Answers0