I am not a good programmer but I think I do everything right.
I have a Sniper Tower and a warrior. As soon as the warrior enters the Sniper’s trigger zone, I add all warriors to the list:
public Rigidbody projectile;
public float timeBetweenShots;
private GameObject warrior;
private float timer;
private List<GameObject> list = new List<GameObject>();
void OnTriggerEnter (Collider other)
{
if(other.tag == "Warrior")
{
GameObject go = other.gameObject;
if(!list.Contains(go))
list.Add(go);
}
}
void OnTriggerExit (Collider other)
{
GameObject go = other.gameObject;
list.Remove(go);
}
And then in OnTriggerStay function in the same script attached to the sniper I use a loop so the sniper shoot at each warrior until the warrior is not destroyed. When destroyed, pick another one and shoot again:
void OnTriggerStay (Collider other)
{
if (other.tag == "Warrior")
{
foreach (GameObject go in list)
{
while (go != null)
{
transform.LookAt (go.transform);
timer += Time.deltaTime;
if (timer > timeBetweenShots)
{
Rigidbody clon = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody;
clon.velocity = transform.TransformDirection (new Vector3 (0, 0, 10));
timer = 0;
print ("Sniper is shooting");
}
}
}
}
}
But no projectile is instantiated, no text is printed and Unity always freezes when any warrior enters the trigger, why? Please help…
In OnTriggerStay you have a while loop inside of foreach. The foreach will give you an item from the list. If that item isn’t null then the while loop will loop forever because you never change the value of go inside the while loop so it remains non-null forever.
I suspect you want an if instead of a while. Or just make sure there aren’t any null values in the list so you don’t even have to do that check. I would suggest the latter.
@xxluky OnTriggerStay will be called up to 60 times per second. You do not need your while loop in there to make this occur every second. Remove it and you’ll find that Unity will no longer freeze, though you’ll be firing way too much. You’ll then need to add additional code to slow your shooter down.
OnTriggerStay is used for physics calculations and can be called multiple times per frame, so you don’t want to run your Snipers AI in this method.
Since you want to manage the fire rate an elegant solution is to use coroutines:
public class Sniper : MonoBehaviour
{
//not entirely sure a sniper's bullet should be using a rigid body, I reccomend
// using raycasting its a lot more accurate (a trait you'd want in Snipers) and
// faster to calculate.
// if you are intent on using rigidbodies the projectile would be better served
// as a gameObject prefab
public GameObject projectile;
public float timeBetweenShots;
private GameObject warrior;
private List<GameObject> warriors = new List<GameObject>();
void Start()
{
StartCoroutine(ComeOutAndPlay());
}
IEnumerator ComeOutAndPlay()
{
while(true)
{
//clean the list from any null gameObjects
warriors.RemoveAll(w=>Utilities.IsNull(w));
if(warriors.Count>0)
{
if(warriors.Contains(warrior))
{
transform.LookAt(warrior.transform);
Instantiate (projectile, transform.position, transform.rotation);
print ("Sniper is shooting");
//this controls the fire rate
yield return new WaitForSeconds(timeBetweenShots);
}
else
{
warrior = warriors[0];
yield return null;
}
}
else
{
yield return null;
}
}
}
void OnTriggerEnter (Collider other)
{
if(other.tag == "Warrior")
{
GameObject go = other.gameObject;
if(!warriors.Contains(go))
warriors.Add(go);
}
}
void OnTriggerExit (Collider other)
{
GameObject go = other.gameObject;
if(warriors.Contains(go))
warriors.Remove(go);
}
}
Almost forgot to mention that I use a utilities IsNull to do all my nullchecks
public static class Utilities
{
//helpful method to check is an object is nulled
public static bool IsNull(object obj)
{
return obj == null || ReferenceEquals(obj,null) || obj.Equals(null);
}
}