- Home /
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);
}
}
Answer by Winterblood · Jan 02, 2012 at 03:49 AM
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: http://answers.unity3d.com/questions/32746/unity-object-array-c.html#
thanks this helpped, i can now get them all to instance properly and move across the screen..
i now have a new problem with the same scripts
in my line of:
foreach (GameObject enemy in Spawner.enemy1) { enemy.transform.position = enemy.transform.position + new Vector3 (speed, 0, 0); }
once i get 2, 3, 4 game enemies moving the speed is being applied to one enemy 4 times..
is there any way to stop this?
You'll find it easier if you work in a more modular fashion. Write a script that handles the movement of ONE enemy, and attach it to your prefab. Then that enemy can store his own speed, target, hit points or whatever, and has his own Update() function.
Ideally you'd just create the instances and leave them to it. When you create the instances you could fill in a reference to your spawner script so that each enemy can tell your spawner when it's about to die, and the spawner can clear that slot in it's array.
This is a popular program$$anonymous$$g philosophy called "Object-Oriented Program$$anonymous$$g" - keeping all the code for object X tucked away inside object X. That's how Unity is designed to work.
thank you i completely forgot about the fact i can just make a game object and drop it int he inspector. i'm use to using XNA through visual studio where i use small lists of objects and update them all at once.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Enemy stops patrolling when player comes close 2 Answers
Enemy moves randomly within a area. 1 Answer
Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers
Unity how to make different enemy spawn one at a time 1 Answer