• 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 xGreyFoxx · Mar 31, 2014 at 12:05 AM · endless loop

Problem with while loop looping infinitely

Okay my while loop is going on forever and im not sure why. This is the list I am accessing in this script

 public List<int> usedTiles = new List<int>(); 


And here is the while loop within the other script. Everything is linking properly as far as I know but it simply loops forever. There must be something very obvious that I am not seeing but I'm sure you guys will.

 int tileIndex = UnityEngine.Random.Range (0, 8);
 while (levelGenerator.usedTiles.Contains (tileIndex))
 {
      tileIndex = UnityEngine.Random.Range (0, 8);
 }         
 
Comment
Add comment · Show 28
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 Lo0NuhtiK · Mar 31, 2014 at 12:09 AM 0
Share

public List usedTiles = new List();

<--That isn't giving you some kind of error to start with? What is usedTiles a List<of> ?

Edit : Never$$anonymous$$d. Thanks for editing your post. 101/010 code button is a pain in the ass sometimes.

avatar image xGreyFoxx · Mar 31, 2014 at 12:13 AM 0
Share

there we go, sorry. First time posting and didn't sere how to include code at first

avatar image Lo0NuhtiK · Mar 31, 2014 at 12:23 AM 1
Share

Here's a link to a random-exclude topic :

http://answers.unity3d.com/questions/452983/how-to-exclude-int-values-from-randomrange.html

The way you have it now, if your [usedTiles] has {0,1,2,3,4,5,6,7} in it then it's going to keep going forever because it'll always generate one of those numbers.

avatar image xGreyFoxx · Mar 31, 2014 at 12:30 AM 0
Share

Well thats the thing, I understand that it would loop forever with those numbers. But I have it empty. I expect that it would loop forever after exhausting its options, but it seems to loop before doing anything, it doesnt appear to do anything before crashing. Is it because it has nothing declared to it? cause, as i understand it, i've left it empty.

avatar image Lo0NuhtiK · Mar 31, 2014 at 12:33 AM 0
Share

You didn't assign anything to it in the inspector either??

avatar image xGreyFoxx · Mar 31, 2014 at 12:39 AM 0
Share

no i did not haha, see I think i am aprroaching this incorrectly

avatar image Bunny83 · Mar 31, 2014 at 12:40 AM 0
Share

It's usually easier to use a list of available elements and you remove them once used ins$$anonymous$$d of tracking which already has been used:
http://answers.unity3d.com/questions/471420/how-to-generate-random-number-that-will-not-be-rep.html

avatar image xGreyFoxx · Mar 31, 2014 at 12:40 AM 0
Share

What i am writing the loop for is to ensure that while placing tiles from the script it only places each one once

avatar image ffxz7ff · Mar 31, 2014 at 01:10 AM 0
Share

Print out the content of the list, see what happens.

avatar image Lo0NuhtiK · Mar 31, 2014 at 01:22 AM 0
Share

It should work without anything being assigned to it.

Is it freezing your editor when you run it?

avatar image xGreyFoxx · Mar 31, 2014 at 01:41 AM 0
Share

It is freezing as soon as I hit play, and that section of code is the only thing that has changed. With that section commented out it runs fine.

avatar image Lo0NuhtiK · Mar 31, 2014 at 01:43 AM 0
Share

Ok, was asking about the freezing because I can't think of any more reasons why it'd loop forever so I was wondering if you were mistaking a constantly randomized number outside of a loop as being an infinite loop.

avatar image xGreyFoxx · Mar 31, 2014 at 01:45 AM 0
Share

Well you may very well be correct, I only assumed that it was in a loop cause it was freezing. Any ideas?

avatar image Lo0NuhtiK · Mar 31, 2014 at 01:52 AM 0
Share

No, I meant that I was asking if it was freezing to verify that the loop is stuck, which it is. I don't know why it's doing that. It should work. Try assigning a few numbers to your list in the inspector and then run it to see if it freezes again or not.

avatar image xGreyFoxx · Mar 31, 2014 at 01:55 AM 0
Share

In the inspector I changed the size to 2 and the elements to 1 and 2, still freezes when run

avatar image xGreyFoxx · Mar 31, 2014 at 01:57 AM 0
Share

I also set the size to 10 and left the elements unassigned but it froze again

avatar image xGreyFoxx · Mar 31, 2014 at 02:01 AM 0
Share

is there something im not declaring at the start of the script maybe?

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
avatar image xGreyFoxx · Mar 31, 2014 at 02:07 AM 0
Share

also as i mentioned before the "usedTiles" list is located in a separate script, if that could have something to do with it. or that I'm using the int variable?

avatar image koray1396 · Mar 31, 2014 at 02:08 AM 0
Share

add a line of debug, so you can be sure that it stucks in the loop. also make sure no numbers are assigned to usedtiles.

avatar image xGreyFoxx · Mar 31, 2014 at 02:17 AM 0
Share

Well this line public List usedTiles = new List(); Is where I declare the list, there shouldnt be anything assigned to it, and it freezes on play so idk how i debug lines would help.

 void Start () 
     {
         tileList.Add(Tile1);
         tileList.Add(Tile2);
         tileList.Add(Tile3);
         tileList.Add(Tile4);
         tileList.Add(Tile5);
         tileList.Add(Tile6);
         tileList.Add(Tile7);
         tileList.Add(Tile8);
 
         GameObject generator = GameObject.Find("LevelGenerator");
         LevelGenerator levelGenerator = generator.GetComponent<LevelGenerator>();
 
 
         int tileIndex = UnityEngine.Random.Range (0, 8);
         while (levelGenerator.usedTiles.Contains (tileIndex))
         {
             tileIndex = UnityEngine.Random.Range (0, 8);
         }                 
 
         int rotationIndex = UnityEngine.Random.Range (0, 4);
         Instantiate ((tileList [tileIndex]), transform.position, Quaternion.Euler (0,(tileRotation[rotationIndex]),0));
         levelGenerator.usedTiles.Add (tileIndex);
     }

There is a more complete picture of whats going on. And as I said with the while loop commented out it runs fine

avatar image Lo0NuhtiK · Mar 31, 2014 at 03:05 AM 0
Share

I don't know...

I just now threw this into a script on one object and dragged some random prefabs into the inspector slots of [tiles] ->

     public List<GameObject> tileList = new List<GameObject>() ;
 public float[] tileRotation = new float[4]; 

 public GameObject[] tiles = new GameObject[8] ;

 void Start () 
 {
     for(int i = 0 ; i < 8 ; i++)
         tileList.Add(tiles[i]) ;
     
     GameObject generator = GameObject.Find("LevelGenerator");
     LevelGenerator levelGenerator = generator.GetComponent<LevelGenerator>();
     
     
     int tileIndex = UnityEngine.Random.Range (0, 8);
     while (levelGenerator.usedTiles.Contains (tileIndex))
     {
         tileIndex = UnityEngine.Random.Range (0, 8);
     }           
     
     int rotationIndex = UnityEngine.Random.Range (0, 4);
 Instantiate ((tileList [tileIndex]), transform.position, Quaternion.Euler (0,(tileRotation[rotationIndex]),0));
     levelGenerator.usedTiles.Add (tileIndex);
 }

Then made another object named "LevelGenerator" and stuck a script on it with public List<int> usedTiles = new List<int>() ; and pushed play... it worked just fine.

Not sure why you're doing this while loop here anyway, but that's beside the point.

avatar image xGreyFoxx · Mar 31, 2014 at 03:20 AM 0
Share

Okay I still can't get it to work. Here is the full code. First Script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TileSpawner : $$anonymous$$onoBehaviour 
 {
     public List<GameObject> tileList = new List<GameObject>();
 
     public GameObject Tile1;
     public GameObject Tile2;
     public GameObject Tile3;
     public GameObject Tile4;
     public GameObject Tile5;
     public GameObject Tile6;
     public GameObject Tile7;
     public GameObject Tile8;
 
     int[] tileRotation = new int[]{90,0,180,270};
 
     void Start () 
     {
         tileList.Add(Tile1);
         tileList.Add(Tile2);
         tileList.Add(Tile3);
         tileList.Add(Tile4);
         tileList.Add(Tile5);
         tileList.Add(Tile6);
         tileList.Add(Tile7);
         tileList.Add(Tile8);
 
         GameObject generator = GameObject.Find("LevelGenerator");
         LevelGenerator levelGenerator = generator.GetComponent<LevelGenerator>();
 
 
         int tileIndex = UnityEngine.Random.Range (0, 8);
         while (levelGenerator.usedTiles.Contains (tileIndex))
         {
             tileIndex = UnityEngine.Random.Range (0, 8);
         }                 
 
         int rotationIndex = UnityEngine.Random.Range (0, 4);
         Instantiate ((tileList [tileIndex]), transform.position, Quaternion.Euler (0,(tileRotation[rotationIndex]),0));
         levelGenerator.usedTiles.Add (tileIndex);
     }
 }

And the Second using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class LevelGenerator : $$anonymous$$onoBehaviour 
 {
     List<GameObject> roomList = new List<GameObject>();
     public GameObject Room1;
     public GameObject Room2;
     public GameObject Room3;
     public GameObject Room4;
     public GameObject Room5;
     public GameObject Room6;
     public GameObject Room7;
     public GameObject Room8;
     public GameObject Room9;
      
     public List<int> usedTiles = new List<int>();
 
     void Start () 
     {
         roomList.Add(Room1);
         roomList.Add(Room2);
         roomList.Add(Room3);
         roomList.Add(Room4);
         roomList.Add(Room5);
         roomList.Add(Room6);
         roomList.Add(Room7);
         roomList.Add(Room8);
         roomList.Add(Room9);
 
 
         for (int i = 0; i <= 8; i++) 
         {
             Instantiate (roomList [i]);
         }
     }
 }
avatar image xGreyFoxx · Mar 31, 2014 at 03:29 AM 0
Share

maybe you can see whats up, the empty object with the "levelgenerator" script is spawn empty objects with the "tilespawner" script. which is why i had the scripts seperate that way.

avatar image Lo0NuhtiK · Mar 31, 2014 at 03:30 AM 0
Share

I copy/pasted both of your full codes just now and didn't modify them at all. Dragged some random prefabs into the Room slots in inspector of the levelgenerator object, then dragged some more into the Tile slots on the tileSpawner... Pushed Play and it fired right up, no problems/errors/nothing.

Edit : Oh.. didn't see your comment above this one when I posted a bit ago. Was just thinking before I looked back in here that maybe you were spawning something that spawned more things that spawned more things.

avatar image Lo0NuhtiK · Mar 31, 2014 at 03:49 AM 0
Share

It goes back to what I said at first about your int list having {0,1,2,3,4,5,6,7} in it and then the random number always generating one of those numbers etc...

Not entirely sure what you're trying to do here or why you're doing it this way, but this is a quick fix to the problem at hand anyway ->

 while (levelGenerator.usedTiles.Contains (tileIndex) && levelGenerator.usedTiles.Count < 8)

Change your while line to that.

avatar image Lo0NuhtiK · Mar 31, 2014 at 03:59 AM 0
Share

Still isn't working perfectly though lmao

avatar image xGreyFoxx · Mar 31, 2014 at 05:26 AM 0
Share

Yeah the way I'm going about things probably isn't the most effecient but I appreciate the help nontheless. This fix is good enough for my purposes as things will be changing as it goes along. Thanks for thye help! I'm sure there will be more posts from me in the future haha

avatar image Lachee1 · Apr 13, 2014 at 08:05 AM 0
Share

The main issue is that it seems that your list contains every value that the Random can give.

you should add a counter and count how many attempts it makes, and stop after X, or even maybe adding a new list and stop once you start repeating the numbers you have already done.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by soulblade · Apr 04, 2014 at 12:03 PM

Try listing your code in the update() function and change 'while' to 'if' that should automatically update the code and test if the used tiles contains the tile index. Hope this helps

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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 avatar image avatar image

Related Questions

While Loop within For Loop crashes Game 1 Answer

Showing a second ObjectPicker as soon as the first one closes freezes the editor 0 Answers

Animation Won't Stop Looping 1 Answer

Coroutine while(bool) kills Unity 1 Answer

Endless runner, road segments spawn speed 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