- Home /
List search gives null
Hello, i've got an issue here and might need some help with it. First my code: using UnityEngine; using System.Collections;
public class AttackTowerAimAndShoot : MonoBehaviour {
public float reload = 1F;
public float firePause = 0.25F;
public GameObject effect;
public float amount = 0.001F;
Transform target;
Transform[] muzzlePositions;
Transform turretTurnPoint;
float newFireTime;
float newMoveTime;
private Quaternion rotation;
private float aimError;
GameObject projectile;
public float range = 500F;
// Use this for initialization
void Start () {
Transform muzzle1 = transform.FindChild("MuzzlePos1");
Transform muzzle2 = transform.FindChild("MuzzlePos2");
turretTurnPoint = transform.FindChild("Turret");
}
// Update is called once per frame
void Update () {
if (target == null)
return;
if (target != null)
{
transform.LookAt(target);
newFireTime = Time.time + (reload * 0.5F);
if (Time.time >= newFireTime)
{
Fire ();
}
}
GetTargetEnemy();
}
void Fire ()
{
newFireTime = Time.time + reload;
newMoveTime = Time.time + firePause;
//CalculateAimError();
for (int i = 0; i < muzzlePositions.Length; i++)
{
projectile = Instantiate (Resources.Load("Bullet"), muzzlePositions[i].position, muzzlePositions[i].rotation) as GameObject;
effect = Instantiate (Resources.Load("FireMuzzle"), muzzlePositions[i].position, muzzlePositions[i].rotation) as GameObject;
}
}
void GetTargetEnemy()
{
for (int i = 0; i < StaticVariables.enemies.Count; i++)
{
if(StaticVariables.enemies[i].transform == null)
{
StaticVariables.enemies.Remove(StaticVariables.enemies[i]);
continue;
}
if(Vector3.Distance (StaticVariables.enemies[i].position, transform.position) < range)
{
//Debug.Log(Vector3.Distance (StaticVariables.enemies[i].position, transform.position));
target = StaticVariables.enemies[i];
break;
}
}
The problem is with the last part: The void GetTargetEnemy(). I want my towers to search through a list for a target. Though, the enemies[i] always returns 'null', so my towers don't get a new target. I don't understand any of it and I get a NullReferenceException at the Vector3.Distance check all the time. Any help or word of advice is welcome. Thanks in advance!
Oh, sorry, that's in another code... $$anonymous$$y list: public static List enemies = new List();
And the line of code in which I fill the List:
void Spawn() { for (int i = 0; i < spawnPoints.Length; i++) { enemy = Instantiate (Resources.Load("Enemy"), spawnPoints[i].position, spawnPoints[i].rotation) as Transform;
StaticVariables.enemies.Add(enemy);
Debug.Log("Enemies " + StaticVariables.enemies.Count);
}
}
Spawning the enemy and adding it directly in the list (I think it does that, at least)
Sorry for the messy script part and in the enemy = Instantiate part enemy is defined as a Transform
I don't see any List in unity, but there is ArrayList (which I think functions the same). Is that what your are using?
@dorpeleg List is a generic list available in Unity and should always be used ins$$anonymous$$d of ArrayList
Answer by whydoidoit · Jan 21, 2013 at 02:38 PM
My guess is that you enemies are getting nulled as they are destroyed.
Given you didn't format your code quite right it's hard to see if your enemy list is
List<Transform> enemies = new List<Transform>();
Presuming it is then I would suggest that you change you loop to look like this:
for(int i = StaticVariables.enemies.Count-1; i>= 0; i--)
{
if(StaticVariables.enemies[i] == null) //Not .transform
{
enemies.RemoveAt(i);
continue;
}
//distance check etcetera
Yes, this does make sense, it's still not working propperly, though... $$anonymous$$y NullReferenceException is gone (and I think I also get the logic behind this For loop) but my 'towers' still don't get a target through... I don't know if this is because I tell the script to look at the distance of the [i] ins$$anonymous$$d of pointing to a Transform object in the first place, but i don't know how to fix this... Is there any way to fix this, or can you give me a hint for another option? Or should i ask this in another question? Anyway: thank you for letting me know the secret behind the NullReference mystery! :D
Answer by Malfegor · Jan 30, 2013 at 08:35 AM
Fixed the problem: I spawned the enemies as Transforms and (I don't know why this happened) they where made null as soon as they entered the list. All i had to do was modify this part:
public Transform enemy;
enemy = Instantiate (Resources.Load("FastEnemy"), spawnPoints[i].position, spawnPoints[i].rotation)
To this:
GameObject newEnemy = Instantiate (Resources.Load("FastEnemy"), spawnPoints[i].position, spawnPoints[i].rotation) as GameObject;
enemy = newEnemy.transform;
And then add it to the list :)