NullReferenceException: Object reference not set to an instance of an object TreeDestroy.Update () (at Assets/Scenes/Trees/TreeDestroy.cs:22)

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


 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);



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

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum