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 frankyboy450 · Mar 20, 2014 at 04:19 PM · gameobjectbugcrashfreeze

My code freezes Unity...

Hello.

I have some code used to pick some random objects from an array and makes sure the same objects isn't picked twice.

Here's my function that get's called to make the array.

 //Used to update the shapes.
     void UpdateShapes ()
     {        
         int count = 3 + (2*currentLevel);
         usedObjects.Clear();
 
         List<GameObject> tempGameObjectsToSpawn = new List<GameObject>();
 
         
         //For each int in Count
         for(int i = 0; i < count; i++)
         {
             //Make a bool called ItemFound and set it to false.
             bool itemFound = false;
 
             //While ItemFound is false(No item has been found)
             while(itemFound == false)
             {
                 //Make a new int.
                 int r = Random.Range(0, shapeObjects.Length);
 
                 //If the list "UsedObjects" contains "r"
                 while(usedObjects.Contains(r))
                 {
                     //Set "r" to something new.
                     r = Random.Range(0, shapeObjects.Length);
                 }
                 //After this. add "r" to the usedObjects list.
                 usedObjects.Add(r);
 
                 //If the list "UsedObjects" doesn't contain the "r" int.
                 if(!usedObjects.Contains(r))
                 {
                     //Then set the itemFound bool to true which stops the code for this object.
                     itemFound = true;
                 }
             }
         }
 
 
         //For each int in usedObjects list
         for(int ii = 0; ii < usedObjects.Count; ii++)
         {
             int index = usedObjects[ii];
             tempGameObjectsToSpawn.Add(shapeObjects[index]);
         }
 
 
         // Fill 2 temp arrays in the end.
         TempClonedObjects = GameObject.FindGameObjectsWithTag("Clone");
         TempSameObjects = GameObject.FindGameObjectsWithTag("Same");
     }

Here's my code used to instantiate all the GameObjects.

 void InstantiateObjects(List<GameObject> objectsToSpawn)
     {
         for (int i = 0; i < objectsToSpawn.Count; i++)
         {
             //Spawn the object(position 0, 0, 0)
             GameObject obj = (GameObject)GameObject.Instantiate(shapeObjects[i]);
 
             // Calculate random values for X,Y,Z and set location of new object
             float x = Random.Range(minX, maxX);
             float y = Random.Range(minY, maxY);
             float z = 0;
             obj.transform.position = new Vector3(x, y, z);
 
             //If it's the last or next last object to spawn, the tag will be set to "Same"
             if(i == objectsToSpawn.Count || i + 1 == objectsToSpawn.Count)
             {
                 obj.tag = "Same";
             }
             // Else set it to "Clone"
             else{
                 obj.tag = "Clone";
             }
         }
     }

I'm not getting any errors in the console so it's weird that it won't work. The program just freezes and won't respond.

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

Answer by Josh707 · Mar 20, 2014 at 04:44 PM

What exactly are you trying to do with these lines?

 usedObjects.Add(r);
  
 if(!usedObjects.Contains(r))
 {
     itemFound = true;
 }

You're adding r to that list and then only ending the loop if it doesn't exist, AFAIK it will always exist and if a loop never ends Unity will freeze. I think just removing ! will make it behave how you want.

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 frankyboy450 · Mar 20, 2014 at 05:14 PM 0
Share

That's for after a random value has been made that isn't already being used, then it sets the ItemFound bool to true and goes to the next GameObject.

I solved my problem though... I forgot to add Break; in the code...

 //For each int in Count
         for(int i = 0; i < count; i++)
         {
             //$$anonymous$$ake a bool called ItemFound and set it to false.
             bool itemFound = false;
 
             
             //While ItemFound is false(No item has been found)
             while(itemFound == false)
             {
                 //$$anonymous$$ake a new int.
                 int r = Random.Range(0, shapeObjects.Length);
 
                 //If the list "UsedObjects" contains "r"
                 while(usedObjects.Contains(r))
                 {
                     //Set "r" to something new.
                     r = Random.Range(0, shapeObjects.Length);
                 }
                 //After this. add "r" to the usedObjects list.
                 usedObjects.Add(r);
 
                 //If the list "UsedObjects" doesn't contain the "r" int.
                 if(!usedObjects.Contains(r))
                 {
                     //Then set the itemFound bool to true which stops the code for this object.
                     itemFound = true;
                 }
                 break; // ----------FORGOT THIS LINE HERE
             }
         }

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

Unity 3D Keep Crashing in every site! 1 Answer

Are coroutines freezing my game? 0 Answers

Unity freezes up completely without explanation 1 Answer

Crash in build when reloading level with a Sprite Renderer in the scene 0 Answers

Unity makes computer freeze when I try to resize the window? 0 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