- Home /
odd error in c# script when working with arrays
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;
}
}
}
}
}
}
Answer by Statement · Jan 02, 2012 at 02:14 PM
transform.position assign attempt for 'enemy1(Clone)' is not valid.
Input position is { 245.247147, 2.169722, -Infinity }.
UnityEngine.Transform:set_position(Vector3)
Your error isn't from your arrays, but from your arithmetics.
enemy.transform.position = enemy.transform.position
- new Vector3 (0, 0, speed / (Spawner.enemyCount + 1));
If enemyCount ever becomes -1, then Spawner.enemyCount + 1
becomes 0. In that case you'll get z evaluating speed / 0
which is infinity. Check how you handle enemyCount and I think you'll be able to narrow down your bug.
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.
Either you should use a for loop instead of a foreach loop, and set the value to null or use Array.IndexOf (but it perform slower since it has to search for it).
// For approach
for (int i = 0; i < Spawner.enemy1.Length; ++i)
{
GameObject enemy = Spawner.enemy1[i];
if (enemy outside border)
{
Spawner.enemy1[i] = null;
}
}
// Foreach approach (slower)
foreach (GameObject enemy in Spawner.enemy1)
{
if (enemy outside border)
{
int index = Array.IndexOf(Spawner.enemy1, enemy);
Spawner.enemy1[index] = null;
}
}
thanks, after some diligiant searching i realized i was hitting -1 some times, so i threw in a catch to stop that.
Answer by roamcel · Jan 02, 2012 at 01:33 PM
Usually, when you have variable array dimensions, you use
List
which is a type from the
System.Collections.Generic
namespace;
And you declare it like:
private List<Transform> mylist;
To populate it with
mylist.Add(mytransform);
It's very useful since you can use lots of important functions on it like 'removeat', 'contains' and others (check msdn reference or just use mono's autocompletion), and you can actually use them like arrays too.
On the other hand, if you just want to use an array, you will have to copy its contents on a new array with modified dimension if you want to add or remove elements. There's no automatic way to do it (best approach is the '.Range' function).
Your answer
Follow this Question
Related Questions
NullReference when accessing GameObject in array (C#) 1 Answer
C# Check If Gameobject is within Collider 1 Answer
press e to speek -1 Answers
Two exact objects behaving differently... *scratches head* 0 Answers
AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers