Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Requiem99 · Aug 29, 2014 at 10:17 AM · crashfreezestartupwhile

Unity crashes when the game starts: maybe due to a while loop

Hi! I have a problem with some scenes in my project. When I start the game Unity freezes and I have to force the quit to close Unity process (Note that this does not always happen and the game starts properly!). After many attempts I discovered that maybe the problem is a series of while loops in my C# script (in fact removing them from the code, the game seems to start always without problems). I have a vector with nine numbers in my Start() class. Than I take a random number from the vector and I save it in a new variable, and so on for eight times. But I don't want that the same number can be stored in two or more different variables, so I wrote this code that contein the while loops. I hope someone can help me fix it so it will work without crashing unity.

     int[] array = { number1, number2, number3, number4, number5, number6, number7, number8, number9 };
     insidearray1 = array[Random.Range(0, 8)];
         for(int i = 0; i < 9; i++)
         {
             if (array[i] == insidearray1)
             {
                 array[i] = 0;
                 break;
             }
         }
         
         insidearray2 = array[Random.Range(0, 8)];
         while (insidearray2 == 0)
         {
             insidearray2 = array[Random.Range(0, 8)];
         }
         for(int i2 = 0; i2 < 9; i2++)
         {
             if (array[i2] == insidearray2)
             {
                 array[i2] = 0;  
                 break;
             }
         }
         
         insidearray3 = array[Random.Range(0, 8)];
         while (insidearray3 == 0)
         {
             insidearray3 = array[Random.Range(0, 8)];
         }
         for(int i3 = 0; i3 < 9; i3++)
         {
             if (array[i3] == insidearray3)
             {
                 array[i3] = 0; 
                 break;
             }
         }

And so on... (All thi is inside the Start() class). Thanks!

Comment
Add comment · Show 1
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 superdudeman · Aug 29, 2014 at 10:54 AM 1
Share

Put a breakpoint inside each of the loops and step through the code to see if it's getting stuck in an infinite loop somewhere.

1 Reply

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

Answer by _MGB_ · Aug 29, 2014 at 10:50 AM

You probably want to look more closely at what Random.Range returns...

 Returns a random integer number between min [inclusive] and max [**exclusive**]

I.e. Random.Range(0,8) will never return 8.

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 Requiem99 · Aug 29, 2014 at 11:28 AM 0
Share

Thanks for the explanation of the function Random.Range. Now the game always seems to start without crashing unity (I've done a lot of attempts and it's always working). It seems strange but I hope that, for some reason, the problem was that. I will test a little more, and if all goes well, the best answer will be yours.

avatar image _MGB_ · Aug 29, 2014 at 01:29 PM 1
Share

Extrapolating your code it's possible that your array becomes full of zeroes up to index 7, whereupon you will get stuck in a while loop as the random int never allows a check of the last item (index 8).

Ed: as an aside, a rather more succinct way to randomise your array (if you are able to use Linq) could be:

 var mixedUp = array.OrderBy(i => Random.value).ToList();
avatar image Requiem99 · Sep 03, 2014 at 10:34 AM 0
Share

Oh, thank you very much again for the advices!

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

24 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

Related Questions

Unity crashes when using while 2 Answers

Editor Hangs after first play using plugin DLL 2 Answers

Game Crashes On Startup? 3 Answers

unity keeps freezing when i run this script,unity keeps crashing whenever i run my daylight cycle script, why? 2 Answers

How can I find editor log file? 5 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