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 mechsrule1 · May 19, 2015 at 07:11 PM · c#unity 5

Script Freezing Unity

So I was working on a bit of coding, and everything else is working fine, but this one bit of code seems to be causing unity some serious problems, and I don't know why. I am using the newest unity build, 5.0.2f1 . It's not even outputting debug.logs (which I removed from here to make it cleaner), it's just eating up more and more ram as it tries to run. the script is called by pressing a GUI button, and here it is. Before you ask, I've done many checks and variations to make sure I'm not doing something stupid like getting caught in an infinite loop. All the values are greater than what I'm trying to subtract from them. To maybe narrow it down, when running the script, if power < OtherPower, it works fine, without freezing.

  void Battle()
  {
     if (power > OtherPower){
         damage = power - OtherPower;
         OtherPower = OtherPower * System.Math.PI;
         }
     if (power < OtherPower) {
             SpawnScript.militia = 0;
             SpawnScript.ManAtArms = 0;
     }
     if (power == OtherPower)
     {
         OtherPower = OtherPower * System.Math.PI;
         SpawnScript.militia = 0;
         SpawnScript.ManAtArms = 0;
     }
     OtherPower = System.Math.Ceiling (OtherPower);
     while (damage >= 1){
             if (damage >= 10 && SpawnScript.ManAtArms > 1){
                 SpawnScript.ManAtArms = SpawnScript.ManAtArms - 1;
                 damage = damage - 10;
             }

             if (damage >= 1 && SpawnScript.militia > 1){
                 SpawnScript.militia = SpawnScript.militia - 1;
                 damage = damage - 1;
             }
             if (damage < 1 || power < 1){
             damage = 0;
             power = SpawnScript.militia + SpawnScript.ManAtArms * 10;
         }

     }
 }
Comment
Add comment · Show 4
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 troien · May 19, 2015 at 07:33 PM 0
Share

Well whenever Unity freezes like that, you usually got yourself an infinite loop. And seeing that you have a while loop there this would be the prime suspect. (maybe another while loop not displayed here could cause it aswell, I don't know the value of power or damage when you run this script, so I can't tell wheter this would freeze it or not)

If it is not infinite, it could be that you are trying to Instantiate a lot of things in the same frame. Though it should in that case start to respond again at some point when it's finished instantiating.

The reason why your Debug.Log is not displaying anything is by the way for the same reason. Because you are most likely stuck in a loop, Unity draws all logs to the display when rendering. Rendering happens at the end of the frame. So if you get stuck somewhere in the frame, it will never log anything.

If you want to debug it, I would advise to change the loop to something like this.

 int maxIterations = 9999; // put a number here that you don't expect to reach
 while (damage >= 1)
 {
     if (damage >= 10 && SpawnScript.$$anonymous$$anAtArms > 1)
     {
         SpawnScript.$$anonymous$$anAtArms = SpawnScript.$$anonymous$$anAtArms - 1;
         damage = damage - 10;
     }
 
     if (damage >= 1 && SpawnScript.militia > 1)
     {
         SpawnScript.militia = SpawnScript.militia - 1;
         damage = damage - 1;
     }
     if (damage < 1 || power < 1)
     {
         damage = 0;
         power = SpawnScript.militia + SpawnScript.$$anonymous$$anAtArms * 10;
     }
     maxIterations--;
     if (maxIterations <= 0)
     {
         Debug.Log("OHOH: " + damage + " : " + power);
         break;
     }
 }

Also, I'm not sure, sinse I don't know what this code is supposed to do, but it could be that you want the second if to be an else if. Because setting both damage to a value greater then 1 and $$anonymous$$anAtArms and militia to 0 will cause an infinite loop. And this is possible if you increase OtherPower enough in the first if get get into the next if.

avatar image mechsrule1 · May 19, 2015 at 10:05 PM 0
Share

Thanks, that helped to at least stop the infinite loop. Still haven't worked out the cause, but I appreciate the help.

avatar image mechsrule1 · May 19, 2015 at 10:11 PM 0
Share

This enabled me to figure out what was going wrong. I needed >= 1 not just >1. That was the cause.

avatar image phxvyper · May 19, 2015 at 10:15 PM 0
Share

@mechsrule1 please put that as the answer to your question.

1 Reply

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

Answer by mechsrule1 · May 20, 2015 at 06:22 AM

The issue was

   if (damage >= 10 && SpawnScript.ManAtArms > 1)
  {
  }
 
  if (damage >= 1 && SpawnScript.militia > 1)
  {
  }

should have been

  if (damage >= 10 && SpawnScript.ManAtArms >= 1)
  {
  }
 
  if (damage >= 1 && SpawnScript.militia >= 1)
  {
  }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Most efficient way to remove a null object from a dictionary 3 Answers

Any way to use multiple font styles within a single UI Text component? 1 Answer

Is it possible to access OnValidate() when using a custom inspector? 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