Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Stonemass · Jan 27, 2021 at 11:09 AM · controllercrashinglooping

Loop fail and unity crash ( eng and spanish )

hi, hola, anyone can explain me why i cant use this condition ? Alguien me puede decir porque no puedo usar esta condicion ?

  private void OnTriggerStay(Collider other)
     {
         if (other.name == "healerTriger")
             Healing();
     }
 
     void Healing()
     {
         takingDmg = false;
         do
         {
             hpPlayer = 1 * Time.deltaTime;
         }
         while (hpPlayer< 50);
            
 
     }

he probado if y while y sigue petando , pero no veo porque ya que la condicion tiene limite. Even whith if or while the condition crash unity but why ? theres a limit in there.... Big Thz

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
1
Best Answer

Answer by NPnpNPnpNP · Jan 27, 2021 at 11:53 AM

You do not need this loop because Healing() will be called every frame. If i understanded what you want to do, this code should work fine.

 private void OnTriggerStay(Collider other)
      {
          if (other.name == "healerTriger")
              Healing();
      }
  
      void Healing()
      {
          takingDmg = false;
 
          if (hpPlayer < 50)
              hpPlayer = 1 * Time.deltaTime;
      }
Comment
Add comment · Show 5 · 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 Stonemass · Jan 27, 2021 at 12:12 PM 0
Share

yep in fact is the same but....we cannot use while loops in unity or what?

avatar image NPnpNPnpNP Stonemass · Jan 27, 2021 at 12:43 PM 0
Share

Yes you can, but it will do all the loop in one frame, so the health will be fully recovered in one frame. Did you check the log of Unity to know a bit more about the crash?

avatar image Stonemass NPnpNPnpNP · Jan 27, 2021 at 01:02 PM 0
Share

i get no error log...unity collapses , crash and die.....ctrl alt sup to recover ;P

Ok thz, finally i fix , i have some problem to understand conditions.... i used this and works but like u said...while operates on a frame, even if the operation is *time.deltatime.... luckily if respects the operation

   while (takingDmg == false && herido == true)
                 Healing();
     
                 //if (takingDmg==false&&herido==true)
                 //         Healing();

avatar image Bunny83 Stonemass · Jan 27, 2021 at 01:08 PM 2
Share

Well, your while loop is an infinite loop. You set hpPlayer to a more or less fixed value which will always be less than 50. Time.deltaTime is a value smaller than 1 which you multiply by 1. So the overall value is less than 1. Since you set your hpPlayer variable to that value, it will never be over 50. What you may wanted to do is

 hpPlayer += 1 * Time.deltaTime;

note the difference to

 hpPlayer = 1 * Time.deltaTime;

In the first case we are "adding" the amount to the variable while in your case you set the value to that amount.


Though even if you want a while loop that completes within one frame, the usage of deltaTime would not make any sense in that case. deltaTime only makes sense for things you do every frame.

avatar image Stonemass Bunny83 · Jan 27, 2021 at 01:23 PM 0
Share

yep thz i got it :)

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

118 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiate Loop Crashing 1 Answer

Why does this script crash Unity? 2 Answers

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

How to decrease gravityScale in period of time? 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