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 Bigproblem01 · Jan 22, 2015 at 10:57 PM · instantiatecrashunityeditorloophang

Why does this code hang the system??

Hi guys,

What I'm tryin to do with the code below is: 1. generate a position within the screen and instantiate a sprite (teleportPrefab) 2. add it to the list 3. then, next time the code is called, check the new generated position against the already existing sprite, if it's satisfactory distance away (in this case it's just 1), then instantiate another sprite and add that to the list too 4. keep generating positions for a new sprite and checking against every other, already instantiated sprite's position, if satisfactory, instantiate

for testing purposes, SpawnTeleport() gets called every half a second just to see if the algorithm works as desired. For K I initially had 1000 and for teleportDistance I had 5 but since Unity would just hang after spawning several teleports I lowered those numbers and it still occurs (sometimes right after instantiating one sprite).

The code doesn't have any perpetual loops and has exits for every case so I don't understand why does it hang the system.

Here's the code:

     public void SpawnTeleport()
     {
         GameObject Teleport;
         Vector2 teleportPos;
         int counter = 0;
         float teleportPosX = Random.Range (0.1f, 0.9f);
         float teleportPosY = Random.Range (0.1f, 0.9f);
         teleportPos = Camera.main.ViewportToWorldPoint (new Vector2(teleportPosX, teleportPosY));
 
         if(TeleportsList.Count > 0)
         {
             for (int k = 0; k < 10; k++) 
             {
                 teleportPosX = Random.Range (0.1f, 0.9f);
                 teleportPosY = Random.Range (0.1f, 0.9f);
                 teleportPos = Camera.main.ViewportToWorldPoint (new Vector2(teleportPosX, teleportPosY));
                 for (int i = 0; i < TeleportsList.Count; i++) 
                 {
                     if(Vector2.Distance(teleportPos, TeleportsList[i].transform.position) >= teleportDistance)
                     {
                         counter++;
                     }
                     else
                     {
                         counter = 0;
                         i = 0;
                     }
 
                     if(counter == TeleportsList.Count)
                     {
                         Teleport = Instantiate (TeleportPrefab, teleportPos, Quaternion.identity) as GameObject;
                         TeleportsList.Add (Teleport);
                         return;
                     }
                 }
             }
             print ("fail");
             return;
         }
         else
         {
             Teleport = Instantiate (TeleportPrefab, teleportPos, Quaternion.identity) as GameObject;
             TeleportsList.Add (Teleport);
             return;
         }
     }


Any help is appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by VioKyma · Jan 22, 2015 at 11:33 PM

The issue lies in this code:

 for (int i = 0; i < TeleportsList.Count; i++)
 {
 if(Vector2.Distance(teleportPos, TeleportsList[i].transform.position) >= teleportDistance)
 {
 counter++;
 }
 else
 {
 counter = 0;
 i = 0;
 }
 ...
 }

Where you are setting the counter = 0;, you are also setting i = 0;, which will reset the progress of the loop. As the state of TeleportsList does not change, you will never exit the loop, as it will continually reach this point and then go back to the start of TeleportsList.

Comment
Add comment · Show 18 · 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 Bigproblem01 · Jan 23, 2015 at 12:07 AM 0
Share

I'm doing that to make sure the newly generated position checks against all the other existing ones. This code won't work like that eventually, i don't need to generate many teleports, maybe 5-6 max.

I get what you're saying I just don't see why finding an appropriate space on screen is so hard when there's so much space left?... Any suggestions on how to go about achieving what I need otherwise?

avatar image Superrodan · Jan 23, 2015 at 12:15 AM 1
Share

I believe what he's saying is that you get your random range before you enter the for loop. So it will never try different spots because it's stuck in the loop. It finds a random spot and the moment that random spot is unavailable you are stuck in an endless loop.

avatar image Bigproblem01 · Jan 23, 2015 at 12:30 AM 0
Share

ah yes, you (and @Vio$$anonymous$$yma) are right, that is a problem there. But if I generate a new position in the else statement (where counter and i are nullified) then I'm back to my old problem - the algorithm generates teleports desired distance away only from the very first one and the rest are spawned wherever in relation to each other... any ideas why that is? Guess I broke it even more with this endless loop by trying to fix that issue

avatar image Superrodan · Jan 23, 2015 at 12:36 AM 0
Share

Here's my idea. I'm not going to write the code but I think the theory checks out. Create a list of Vector2 in code: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

Each list needs to remember the x and y position of the teleport. Whenever you place a new teleport, add it's Vector2 to the list. Each time you check whether a new one is too close to the old one cycle through the entire list and check for all of them. If you are too close to any of them reset the random variables and try again.

avatar image Superrodan · Jan 23, 2015 at 12:39 AM 0
Share

One note to keep in $$anonymous$$d is that in theory you can still hang your device for a while if it keeps generating numbers too close to your current teleports.

There is probably a more efficient way than generating random numbers until they are not too close to other random numbers for this but I don't know what it is. You'll need a better programmer than I.

Show more comments
avatar image
0

Answer by hav_ngs_ru · Jan 22, 2015 at 11:24 PM

where do you call SpawnTeleport first? Let me guess.... hmmm... in Update? :)) I'm joking of cause, but you can never tell what might happen :))

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 Bigproblem01 · Jan 23, 2015 at 12:03 AM 0
Share

@hav_ngs_ru as a matter of fact, it is from Update(), If a certain bool becomes true, then there's a timer in update which decreases and when it reaches zero it calls SpawnTeleport() and resets itself to call it again 0.5f seconds later. That bool becomes false after certain point, never to become true 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

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

OnCollisionEnter2D() Instantiate Causing an Infinite Loop 0 Answers

Loop crashing unity (pathfinding) 2 Answers

Instantiate objects in a specific order 2 Answers

Unity hangs packing sprites for Android compressed best quality 2 Answers

How can I make sure my app actually closes when I manually shut it down through iOS? 1 Answer


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