i’m working with arrays of game objects and when i set an array element to be destroyed i know it should goto a null value and i’m even skipping the null value elements (rather than rebuild and shrink the array down (which i’m thinking about doing.)
the error i get is:
transform.position assign attempt for ‘enemy1(Clone)’ is not valid. Input position is { 245.247147, 2.169722, -Infinity }.
UnityEngine.Transform:set_position(Vector3)
i found a work around to set from center to pivot and this worked for the first element in the array but not for anything else.
so i guess i ask, how would i easily loop through an array of game objects and update a position and remove the element from the array when it reaches a border.
the array is being set in another script.
this is my current script:
using UnityEngine;
using System.Collections;
public class movement1 : MonoBehaviour {
public float speed = .25f;
public GameObject stopper;
public static float time = 0;
public static float timer = 15;
public GameObject[] enemies;
public bool deathSub = false;
public bool destroy = false;
public int count = 0;
// Use this for initialization
void Start () {
stopper = GameObject.FindGameObjectWithTag("EnemyDeath1");
}
// Update is called once per frame
void Update () {
if (MainGUI.stopped == false)
{
count = Spawner.enemyCount;
if (Spawner.creating == true)
{
foreach (GameObject enemy in Spawner.enemy1)
{
if (enemy != null)
{
if (destroy == false)
{
enemy.transform.position = enemy.transform.position - new Vector3 (0, 0, speed / (Spawner.enemyCount + 1));
}
if (enemy.transform.position.z <= stopper.transform.position.z)
{
destroy = true;
}
if (destroy == true)
{
Destroy(enemy);
deathSub = true;
destroy = false;
}
if (deathSub == true)
{
Spawner.enemyCount--;
deathSub = false;
}
}
else
{
continue;
}
}
}
}
}
}