Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xxluky · Feb 22, 2016 at 04:51 PM · c#loopfreezing

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
     }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxluky · Feb 22, 2016 at 07:53 PM 0
Share

Perfect! Exactly what I needed. The code is a little bit complex to me but good to learn it. Thank you so much.

avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxluky · Feb 22, 2016 at 05:31 PM 0
Share

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.

avatar image bitstrider · Feb 22, 2016 at 05:46 PM 0
Share

OnTriggerStay is called for every frame between OnTriggerEnter and OnTriggerExit. You dont need the while

avatar image xxluky · Feb 22, 2016 at 06:08 PM 0
Share

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...

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges