Unity always freezes using this code, why?
Hi guys,
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...
Answer by JoshuaMcKenzie · Feb 22, 2016 at 07:00 PM
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);
}
}
Perfect! Exactly what I needed. The code is a little bit complex to me but good to learn it. Thank you so much.
Answer by Dave-Carlile · Feb 22, 2016 at 05:24 PM
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.
Yeah, but I want the sniper shoot every second untill the warrior is not destroyed. When he is destroyed, he will be null, or not? How should I do it? Now I am confused. I thought when I destroy a gameobject it would be null... The warrior has a health. When he ran out of health he is destroyed.
OnTriggerStay is called for every frame between OnTriggerEnter and OnTriggerExit. You dont need the while
Thanks for you interest, guys. But when I remove the while it behaves the way I do not want to. For example if three warriors enter the sniper's trigger, the sniper first of all turns towards the first warrior and shoots. After that, the sniper turns to the second warrior and shoots and so on...
I want to kill the first warrior and after that turn to the second one and kill that one and so on...
Answer by Magius96 · Feb 22, 2016 at 09:52 PM
@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.
Your answer
Follow this Question
Related Questions
Break the loop when there are no more questions 0 Answers
Unity update does not be called when lock screen 0 Answers
Function runs fine the first time but crashes if I run it twice? 1 Answer
if ALL items in array are something 1 Answer
How can I check the color of GameObjects in an Array and print out their color? 0 Answers