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 katie88 · Jul 27, 2014 at 05:11 PM · crashloop

Loop to populate array crashes Unity

I am trying to write a loop that populates my array "tileNumberArray" with unique, random numbers. I am aware that there are already quite a few solutions posted on Unity Answers that involve shuffling, etc. I may have to revisit those ideas, but I currently can't get any of them to work because the syntax I've found has so far been unrecognized by Unity.

What I have pasted below almost works. It:

1) Generates a random number 2) Checks to see if that random number is already in tileNumberArray 3) If so, it calls the "pickNewNumber" function, which will loop until it finds a number that isn't used yet.

When it finds an unused number, it exits the while loop without assigning the "randomNumber" to the current index in "tileNumberArray".

The assignment statement:

tileNumberArray[i] = randomNumber;

HAS to be within that else clause at the end of my Start() function. It seems that if I put it anywhere else, Unity crashes. If it has to generate a new random number, I want it to assign the number before exiting the while loop and have it continue and finish populating my array. But this way, it just skips assigning if it runs into duplicates.

I'm probably missing something obvious... And if this solution is too inefficient or convoluted, I will try to get other methods to work.

Thank you in advance!


 //Array to hold randomly-generated numbers
 var tileNumberArray = new Array(4);
 
 //Initialize the loop variable
 var i = 0;
 
 var randomNumber = 0;
 
 var numberAlreadyExists = false;
 
 
 
 function Start () {
 
 //Populate the tileNumber array with numbers.
 for (i = 0; i < tileNumberArray.length; i++){
 
 //Generate a random number between 1 and 4
 randomNumber = Random.Range(1, 4);
 
     //If this number already exists in the array, generate another random number
     if (randomNumber in tileNumberArray){
 
     Debug.Log("Random Number " + randomNumber + " is already in the array");
 
     numberAlreadyExists = true;
 
     pickNewNumber();
 
     Debug.Log("New number is: " + randomNumber);
 
     }
 
 
     else{
         
     tileNumberArray[i] = randomNumber;
 
     Debug.Log("Tile " + i + " is " + tileNumberArray[i]);
     }
 
 }
 }
 
 function pickNewNumber(){
 
 while (numberAlreadyExists == true){
     
     Debug.Log("Finding unused number...");
 
     //Generate a random number between 1 and 4
     randomNumber = Random.Range(1, 4);
 
         if (randomNumber in tileNumberArray){
 
         Debug.Log("New Random Number " + randomNumber + " is already in the array.");
         }
         
         else {
         
         Debug.Log("New Random Number " + randomNumber + " is NOT in the array");
         
         numberAlreadyExists = false;
         }
         }
         
     
     }
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 katie88 · Jul 27, 2014 at 05:23 PM 0
Share

Because my question is probably overly confusing, here is an example from my console after running the above script.

alt text

unity.png (75.8 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Jul 27, 2014 at 05:59 PM

Some things:

1) Never use the Array class; it's slow, untyped, and will cause problems. Use built-in arrays or a generic List.

2) Only declare variables inside functions, unless they absolutely must be shared between functions.

And the big one:

3) Random.Range (1, 4) doesn't pick a number between 1 and 4. See the docs; Range with ints is exclusive, so it only picks between 1 and 3.

You'd be better off using a shuffle bag algorithm rather than using while loops like this.

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 katie88 · Jul 28, 2014 at 12:05 PM 0
Share

Thank you for your quick and helpful response! Yeah I'm definitely not an expert at efficient program$$anonymous$$g (or ... in general), as you can see, haha. And I was noticing that I was never seeing 4 with my range results but was putting off looking into that. I will look into your suggestions. Thank you again!

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 doing A* pathfinding loop 1 Answer

Infinite Looping Crash 2 Answers

Unity not picking up errors until after reopening 0 Answers

Infinite loop crash System.Enum.ToString() 0 Answers

Unity freezes when OnTriggerEnter occurs 3 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