c# array of game objects movement help

i am building a game that will have 2 arrays of game objects that will move on the screen. I can get the array to build the objects when told to… i am having issues with looping through the array through another script and moving the objects.

i get alot of unexpected results such as null reference, or outside of range.

i’m sure it’s something very simple i’m missin, i’ve tried for loops and foreach loops with no success… i’ve even copied the array locally, but still didn’t give me the correct results.

any and all help would be great on this.


Enemy Spawning script:

public int time = 0;

public int spawnTimer = 225;

public int trueCount = 0;

public static int waveCount = 10;

public static int waveCounter = 30;

public static int enemyCount = 0;

public GameObject enemyPrefab1;

public GameObject enemyPrefab2;

public static GameObject enemy1;

public static GameObject enemy2;

public bool created = false;

// Use this for initialization
void Start () {
	created = false;
	enemy1[trueCount] = Instantiate(enemyPrefab1,
									enemyPrefab1.transform.position,
									enemyPrefab1.transform.rotation)
									as GameObject;					
	enemy2[trueCount] = Instantiate(enemyPrefab2,
									enemyPrefab2.transform.position,
									enemyPrefab2.transform.rotation)
									as GameObject;
}

// Update is called once per frame
void Update () {
	if (time >= spawnTimer)
	{
		time -= spawnTimer;
		created = true;
		if (created == true)
		{
			if (trueCount < waveCount)
			{
				enemy1[trueCount] = Instantiate(enemyPrefab1,
												enemyPrefab1.transform.position,
												enemyPrefab1.transform.rotation)
												as GameObject;					
				enemy2[trueCount] = Instantiate(enemyPrefab2,
												enemyPrefab2.transform.position,
												enemyPrefab2.transform.rotation)
												as GameObject;
				trueCount ++;
				enemyCount ++;
				created = false;
			}
		}
	}
	else
	{
		time++;
		created = false;
	}
}

movement script1:

public float speed = .25f;

public GameObject stopper;

public static float time = 0;

public static float timer = 15;

public GameObject enemies;

// Use this for initialization
void Start () {
	stopper = GameObject.FindGameObjectWithTag("EnemyDeath1");
}

// Update is called once per frame
void Update () {
	foreach (GameObject enemy in Spawner.enemy1)
	{
		enemy.transform.position = enemy.transform.position - new Vector3 (0, 0, speed);
	}
}

movement script2:

public float speed = .25f;

public GameObject stopper;

public static float time = 0;

public static float timer = 15;

public GameObject enemies;

// Use this for initialization
void Start () {
	stopper = GameObject.FindGameObjectWithTag("EnemyDeath2");
}

// Update is called once per frame
void Update () {
	foreach (GameObject enemy in Spawner.enemy1)
	{
		enemy.transform.position = enemy.transform.position + new Vector3 (speed, 0, 0);
	}
}

public static GameObject enemy1;

…is declaring a reference to an array of GameObjects that doesn’t exist, because you haven’t yet told it how big it should be. You need to create the array for it to reference before you can fill in any entries:

void Start () {
    enemy1 = new GameObject[8];
}

Also see this response for multidimensional arrays: Unity Object Array C# - Questions & Answers - Unity Discussions