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_.position, muzzlePositions*.rotation) as GameObject;*_

effect = Instantiate (Resources.Load(“FireMuzzle”), muzzlePositions_.position, muzzlePositions*.rotation) as GameObject;
}
}
void GetTargetEnemy()
{
for (int i = 0; i < StaticVariables.enemies.Count; i++)
{
if(StaticVariables.enemies.transform == null)
{
StaticVariables.enemies.Remove(StaticVariables.enemies);
continue;
}*_

_ if(Vector3.Distance (StaticVariables.enemies*.position, transform.position) < range)
{
//Debug.Log(Vector3.Distance (StaticVariables.enemies.position, transform.position));
target = StaticVariables.enemies;
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 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!

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 *== null) //Not .transform*

{
enemies.RemoveAt(i);
continue;
}
//distance check etcetera

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_.position, spawnPoints*.rotation)*_

To this:
GameObject newEnemy = Instantiate (Resources.Load(“FastEnemy”), spawnPoints_.position, spawnPoints*.rotation) as GameObject;
enemy = newEnemy.transform;*

And then add it to the list :slight_smile:_