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 coltonatlas · Aug 30, 2018 at 06:08 PM · whilewhile-loopwhile looponcollisionstayoncollision

While loop causing Unity to hang up?

     void OnCollisionEnter(Collision colInfo)
     {
         if (colInfo.collider.tag == "Infection")
         {
             Debug.Log("IF STATEMENT");
             health = health - 1;
             Die();
         }
     }
 
     void OnCollisionStay(Collision stayInfo)
     {
         while (stayInfo.collider.tag == "Infection")
         {
             timer = 0;
             timer += Time.deltaTime;
             if(timer == 1)
             {
                 health -= 1;
                 timer = 0;
             }
             Die();
             Debug.Log("WHILE STATEMENT");
         }        
     }
 
     void Die()
     {
         if (health <= 0)
             {
                 Destroy(gameObject);
                 Instantiate(infection, transform.position, transform.rotation);
             }
     }

Not exactly sure why it's having issues... I read to be careful with while statements and this really doesn't seem like it's running infinitely. It runs fine with the if statement in OnCollisionEnter but doesn't work with the while statement in OnCollisionStay.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by eses · Aug 30, 2018 at 06:20 PM

hi @coltonatlas


If you check the manual for OnCollisionStay:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay.html

There it says:
"OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider."

So every frame your collision happens, your while loop gets called. Not exactly what you want most likely.

Here is a modified example, that causes typical contact damage.

Each frame player has contact to some specific collider, counter value goes up. After a while timer gets full, and hitpoint is reduced. Timer is zeroed, and starts again.

If player stays in contact long enough, he runs out of energy and dies.

 public float health = 10f;
 public float timer = 0f;
 
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Infection")
     {
         timer = 0f;
     }
 }
 
 
 void OnTriggerStay(Collider other)
 {
     // Every frame overlapping infected
     SufferInfection();
 }
 
 
 void SufferInfection()
 {
     timer += Time.deltaTime;
 
     // On timer full, cause damage
     if (timer >= 1f)
     {
         health -= 1f;
         timer = 0;
     }
     
     // When
     if (health <= 0) Die();
 }
 
 
 void Die()
 {
     Debug.Log("You died!");
 }
Comment
Add comment · Show 6 · 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 coltonatlas · Aug 30, 2018 at 06:26 PM 0
Share

Well damn. All I'm really trying to do is start a timer while contact between two bodies exist. I figured OnCollisionStay would be the right thing to do. Do you have any advice? Thank you for the info on OnCollisionStay!

avatar image eses coltonatlas · Aug 30, 2018 at 06:34 PM 0
Share

@coltonatlas - well you should (like I had to) rubber duck this... i.e. explain the code to yourself, that way you'll eventually figure out what is wrong. Also, work with pseudo code first, that way it is easier to avoid this kind of traps.

avatar image eses coltonatlas · Aug 30, 2018 at 06:35 PM 0
Share

I also think, you are better off using trigger. You most likely will bounce away from contact, which will cause timer to stop and so on...

avatar image eses coltonatlas · Aug 30, 2018 at 06:41 PM 0
Share

@@coltonatlas - I added a code example.

avatar image coltonatlas eses · Aug 30, 2018 at 07:16 PM 0
Share

Thanks! That helped a lot. The only issue it's having now is that the timer seems to just stop working after a few triggers. They'll hit and bounce off of each other a few times but will slowly just stay touching due to them going after each other. The timer works fine, then just seems to stop counting after a few triggers happening. :/

Show more comments

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

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

[Solved]Cant Find loop that freezes Unity 2 Answers

Why is this Do/While loop hanging Unity? 1 Answer

Do-While Loop not working 1 Answer

NEED HELP PLEASE: When i kill a one of the three limited "mobSpawned" the "while (true)" don't work and don't spawn a new object. Anyone have a solution for this. Thanks. 1 Answer

While Loop not working when clicked on Play button 1 Answer


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