Quantcast
Channel: Questions in topic: "write-my-code"
Viewing all articles
Browse latest Browse all 92

What's wrong with this code and is there an easier way to write it!?

$
0
0
Hi, all I want to do is instantiate a prefab I already have setup at a random spawn point. The spawn points are just Empty Game Objects and there are a few of them setup on my map. I have written this code: #pragma strict private var spawnNum = 0; function Awake () { spawnTesting(); } function spawnTesting () { WaitForSeconds(spawnNum * 50); spawnNum = Random.Range(0, 1); numCheck(); } function numCheck() { if(spawnNum <= 0.1) { Debug.Log("Spawn1"); spawnTesting(); } else if (spawnNum <= 0.2) { Debug.Log("Spawn2"); spawnTesting(); } else if (spawnNum <= 0.3) { Debug.Log("Spawn3"); spawnTesting(); } else if (spawnNum <= 0.4) { Debug.Log("Spawn4"); spawnTesting(); } else if (spawnNum <= 0.5) { Debug.Log("Spawn5"); spawnTesting(); } else if (spawnNum <= 0.6) { Debug.Log("Spawn6"); spawnTesting(); } else if (spawnNum <= 0.7) { Debug.Log("Spawn7"); spawnTesting(); } else if (spawnNum <= 0.8) { Debug.Log("Spawn8"); spawnTesting(); } else if (spawnNum <= 0.9) { Debug.Log("Spawn9"); spawnTesting(); } else if (spawnNum <= 1) { Debug.Log("Spawn10"); spawnTesting(); } else { spawnTesting(); } } So all I am doing is when the game is started, Awake calls a function to start a cycle to check a Random.Range and then calls a debug.log which will eventually be an instantiate moving it to the specific spawn point. I think I have made some sort of infinite loop because my Unity keeps 'Not Responding' when I try to test the game. I am new to coding and would like to know if the way I am checking for the Random numbers is efficient enough or could be shortened and also how I can or could re write the code so I don't get an infinite loop going. Thanks!

Viewing all articles
Browse latest Browse all 92

Trending Articles



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