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!