Accessing List in Instantiated (Clone) Game Object from Another Script in C#

So basically I have three related classes, an EnemyGenerator, an Enemy, and a Points Controller script.

The enemy generator is in a prefab that is instantiated at runtime at a random location. Then the Enemy prefabs are instantiated from within that script at various times throughout the game.

Inside the EnemyGenerator script I have a List that I store created enemies in.

I would like to access it via the Points Controller script, but every time I try it shows up as being empty. But if I check the List in the EnemyGenerator prefab, everything is working as expected, the List is being populated with the enemies as they are spawned.

The code for the Points Controller script goes something like this.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PointsController: MonoBehaviour {

public List<GameObject> enemyArray = new List<GameObject>();

void Start () {
enemyArray = GameObject.Find("EnemyGeneratorPrefab").GetComponent<EnemyGenerator>().EnemyArray;
}

void Update(){
    Debug.Log(enemyArray.Count);
}
}

I know this is probably something really simple, but it has been driving me crazy for a few hours now. Any help would be greatly appreciated. Thanks in advance!

Post the code for your EnemyGenerator script.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class EnemyGenerator: MonoBehaviour {
GameObject loadedEnemy;
public List<GameObject> enemyArray = new List<GameObject>();

IEnumerator GenerateEnemy(){
    for(int counter = 0; counter <= numOfEnemies; counter++){
        generatedEnemy = (GameObject)Instantiate(loadedEnemy, bounds, Quaternion.identity);
        enemyArray.Add(generatedEnemy);
}

Void Awake(){
    loadedEnemy = (GameObject)Resources.Load("Enemy");
    StartCoroutine(GenerateAnt());
}

}
}

In EnemyGenerator:
numOfEnemies is not declared
generatedEnemy is not declared
bounds is not declared.
you capitalized Void. (should be void, lowercase)
Awake is within GenerateEnemy
and GenerateAnt isn’t valid.

In PointsController, you do GetComponent.EnemyArray, which is not defined. Try enemyArray instead (case sensitive).

Also the coroutine will have no effect, without a yield it’ll do the whole loop in one frame. do a yield at the end of each loop.

Try this correction, maybe. using UnityEngine;using System.Collections;using System.Collections.Generic; - Pastebin.com

Sorry that was my fault, I didnt include all the code, I tried to just include the relevant pieces but i see that i left a lot out. All the code works so far without any errors its just the when I try and access the List from another class it doesnt seem to work right because it shows up empty.

Here you have EnemyArray with a capital E:

enemyArray = GameObject.Find("EnemyGeneratorPrefab").GetComponent<EnemyGenerator>().EnemyArray;

but here you declare it without a capital A:

public class EnemyGenerator: MonoBehaviour {
...
public List<GameObject> enemyArray = new List<GameObject>();

I’m surprised you don’t get any errors.

It is because i retyped it here and changed the names to make it easier to follow. All I am trying to figure out how to properly access the list and print the Count from another script.

is there more than one “EnemyGeneratorPrefab” in your hierarchy?

You really should just post your code line-for-line from your code editor. It’s ridiculous to expect us to help you properly otherwise.

yeah would be nice, less guessing if it’s a bug in your code our just a mistake in shorten your code.

But guessing when you get an empty list is

  1. Your PointsController is also active from beginning and you are accessing the array in Start(), i don’t know if you get a reference to this array or just a copy. So that in start the array in your EnemyGenerator is still count 0.
  2. You have more than one EnemyGeneratorPrefab in your hierarchy and you are getting the wrong.
  3. EnemyGeneratorPrefab is not active in hierarchy

but it’s just guessing not seeing all code.

Okay, sorry for the confusion, I will post the full code. Hopefully I didn’t miss anything/jumble anything up this time.

EnemyGenerator // this is on a prefab

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// TODO create a spawn point generator for random locations OUTSIDE the map

public class EnemyGenerator : MonoBehaviour {

   public int EnemiesPerSecond = 0;
   public int SpawnEveryXSeconds = 0;
   GameObject generatedEnemy;
   //Vector3 EnemyGenLocation;
   public List<GameObject> arrayOfEnemies;
   GameObject loadedEnemy;
   Vector3 bounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0.0f));

   IEnumerator GenerateEnemy(){

     while (true) {
       for (int counter = 0; counter <= EnemiesPerSecond; counter++){
         bounds.Set((bounds.x + Random.Range(0.0f, 3.0f)), (bounds.y + Random.Range(0.0f, 3.0f)), bounds.z);
         generatedEnemy = (GameObject)Instantiate(loadedEnemy , bounds, Quaternion.identity);
         arrayOfEnemies.Add(generatedEnemy); // add generated enemy to the List
         counter++;
         yield return new WaitForSeconds(0.07F);
       }
       yield return new WaitForSeconds(SpawnEveryXSeconds);
     }
   }

   void Awake () {
     loadedEnemy = (GameObject)Resources.Load("Enemy");
     StartCoroutine(GenerateEnemy());
   }
   
   // Use this for initialization
   void Start () {

   }
   
   // Update is called once per frame
   void Update () {
     Debug.Log(arrayOfEnemies.Count); // this displays the correct number of instantiated enemies
   }
}

PointsController // stuck this on an empty game object in the scene

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PointsController : MonoBehaviour {
   public List<GameObject> enemyArray = new List<GameObject>();

   void Awake () {

   }

   void Start () {
     enemyArray = GameObject.Find("EnemyGeneratorPrefab").GetComponent<EnemyGenerator>().arrayOfEnemies;
   }

   void Update () {
     Debug.Log (enemyArray); // this one shows up as 0 every time
   }

}