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 DarKTower · Jun 03, 2014 at 04:29 AM · mathlistsinteger

Int List won't let me add an Int Variable

Quick summary:

I am randomizing a variety of variables, and then storing the ID of these values in an Int List in the script as they are generated. It generates 10 in a row, and checks against the Int List to see if the ID has been taken already. If the Int List contains the ID already, then a new ID is generated, checks against the Int List, and continues. When the ID is not found inside the Int List it .Add()'s to the Int List and continues generating other information.

I am having a problem with the .Add(). For some really confusing reason, every .Add() I call works perfectly except when the Int has been randomized more than once. If this is confusing, here is my code:

 //KEEP A LOG OF ALL DATABASE ENTRIES USED, TO NOT ALLOW DUPLICATES
 private var nameLog: List.<int> = new List.<int>();
 
 //TESTING VALUES
 private var tripleRandom: int = -1;
 
 function Start() {
     for (var i=0;i<10;i++) {
         generateInfo(); //GENERATE 10
     };
 }
 
 function reRandomizeFeature(totalAmount:int) {
     var rerandomize: int = Random.Range(0, totalAmount); //RANDOMIZE A NEW NUMBER
     Debug.Log('RANDOMIZED A '+rerandomize);
     if (nameLog.Contains(rerandomize)) { //IF THE LIST CONTAINS IT, RE-RANDOMIZE IT AGAIN
         Debug.Log('Contains '+rerandomize+'. Rerandomize it again!');
         reRandomizeFeature(totalAmount);
     } else { //IF IT DOESN'T, ALLOW IT THROUGH
         Debug.Log('Randomized '+rerandomize+' and nameLog does not contain it. Allow it to go through!');
         tripleRandom = rerandomize;
     }
 }
 
 function generateInfo() {
     var char_nameDBLength: int = 15; //15 TOTAL
     var nameSelectedByRandom: int = Random.Range(0, char_nameDBLength); //GENERATE THE FIRST ATTEMPT
 
     Debug.Log('------------------------------');
     if (nameLog.Contains(nameSelectedByRandom)) { //IF LIST CONTAINS THE RANDOMED ID
         Debug.Log('Contains '+nameSelectedByRandom+'. Randomizing a new ID.');
         reRandomizeFeature(char_nameDBLength); //RANDOMIZE A NEW ID
     } else {
         Debug.Log('Does not contain '+nameSelectedByRandom+'. No need to randomize');
     }
 
     Debug.Log('Saving ID '+nameSelectedByRandom+' ('+tripleRandom+') to the NameLog.');
 
     if (tripleRandom <= -1) { //IF THE ID DID NOT HAVE TO BE RANDOMIZED AGAIN
         nameLog.Add(nameSelectedByRandom); //ADD TO THE LIST
         Debug.Log('Do not add Triple Random, add: '+nameSelectedByRandom);
     } else { //IF THE ID HAD TO BE RANDOMIZED (AND THE TEMP VAR IS ABOVE -1), ADD IT TO THE LIST
         Debug.Log('Add Triple random: '+tripleRandom);
             //THIS IS WHERE I GET THE PROBLEMS:
         nameLog.Add(tripleRandom); // IF THIS LINE IS HERE, UNITY COMPLETELY FREEZES AND I HAVE TO CLOSE AND REOPEN.
             //IF THIS LINE IS COMMENTED OUT, THE CODE WORKS BUT NOTHING IS ADDED TO THE INT LIST.
     }

     Debug.Log(nameLog[nameLog.Count-1]); //PRINT THIS ID

     tripleRandom = -1; //RESET THE TEMP ID BACK TO -1 TO BE RESET
     }

The commented line is where the problem arises. I have been fighting to try and figure this out for hours now, and I am just hung up on this tiny little line of code. The annoying thing is if I comment out the nameLog.Add(tripleRandom), and add in "nameLog.Add(5)" or anything like that, the code works perfectly fine. It seems it's the damn variable that is the problem, but I don't understand what's wrong.

Any help would be amazing, thank you.

Comment
Add comment · Show 2
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 HarshadK · Jun 03, 2014 at 05:16 AM 0
Share

Doesn't seem to be a problem there. Works fine at my end.

avatar image DarKTower · Jun 03, 2014 at 06:15 AM 0
Share

Well that's weird... For me it freezes Unity and forces me to Force Quit and reopen. I mean it works perfectly without the nameLog.Add(tripleRandom) line, but as soon as I add that it freezes. Very aggravating...

1 Reply

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

Answer by fafase · Jun 03, 2014 at 06:23 AM

If I get it right, you want a list of values that are unique and in your process you generate a value and if that value already exists, you go for creating a new one.

Your problem lies in your recursive process that you do not control the iteration. This is common problem that you either need to define a sentinel that will tell after x amount of iteration, the process stops but you may end up with no result.

See, you can get 15 values and you want to get 10 out of that, if on the five first your get 7,10,5,6,3 for instance, you already have 33% chances to go on a endless loop and this value will increase every time you manage to find a non existing one. For the last value you have a 60%(9/15) chance to get screwed on each trial to find a new value.

Now for a solution:

  1. Store all possible values in a list

  2. Get a random value between 0 and list.Count (list.Count will be excluded)

  3. Get the value from the list at the index of value got in step2

  4. Put it in your valueList and remove it from list

  5. Repeat process until you have 10 numbers

Since you remove the chosen value when you get it, you won't get it again.

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 DarKTower · Jun 03, 2014 at 06:49 AM 0
Share

I had a feeling it was the way I was looping, even when it did work it was very slow to start. This method is much quicker and (Obviously) not prone to any infinite loops. Thank you so much!

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

21 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

Related Questions

Combination of a list - no repeats, specify length 1 Answer

merge sort crash Unity 2 Answers

Really weird behaviour with boolean variables from custom class 2 Answers

C# divide float by integer 2 Answers

How to convert and integer into an Array of his digits 4 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