Quantcast
Channel: Questions in topic: "crash"
Viewing all articles
Browse latest Browse all 2383

Why does this code hang the system??

$
0
0
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!

Viewing all articles
Browse latest Browse all 2383

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>