Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

trying to instantiate an object a random number of times ,how do i spawn something in a random ammount of times?

$
0
0
trying to instantiate an object set random number of times but it just loops infinitely using System.Collections; using System.Collections.Generic; using System.Data; using System.Globalization; using UnityEngine; public class Spawner_Script : MonoBehaviour { public static bool night = false; public static int buildings = 2; public static int armysize; public int monsters; public static bool daytime = false; public GameObject devil; public static Transform spawner; //Money = House_Timer.money; // Start is called before the first frame update void Start() { daytime = Day_Night_Cycle.isMonstersDead; int devnum = Random.Range(0, 6); } // Update is called once per frame void Update() { buildings = Var_Controller.population; night = Day_Night_Cycle.isNight; if (night) { //generate max monsternum //randomise army //spawn armysize = buildings * 2; monsters = armysize; //Debug.Log(monsters); //Debug.Log(armysize); int iterations = Random.Range(0, 11); for (var i = 0; i < iterations; i++) { GameObject d = Instantiate(devil, spawner); } //d.transform.position = new Vector2(spawner); } if (night) { if(monsters <= 0) { daytime = true; } } } } ,I've tried doing the for loop thing that I've seen on other posts but it just doesn't work all it does is spawn game objects in infinitely using System.Collections; using System.Collections.Generic; using System.Data; using System.Globalization; using UnityEngine; public class Spawner_Script : MonoBehaviour { public static bool night = false; public static int buildings = 2; public static int armysize; public int monsters; public static bool daytime = false; public GameObject devil; public static Transform spawner; //Money = House_Timer.money; // Start is called before the first frame update void Start() { daytime = Day_Night_Cycle.isMonstersDead; int devnum = Random.Range(0, 6); } // Update is called once per frame void Update() { buildings = Var_Controller.population; night = Day_Night_Cycle.isNight; if (night) { //generate max monsternum //randomise army //spawn armysize = buildings * 2; monsters = armysize; //Debug.Log(monsters); //Debug.Log(armysize); int iterations = Random.Range(0, 11); for (var i = 0; i < iterations; i++) { GameObject d = Instantiate(devil, spawner); } //d.transform.position = new Vector2(spawner); } if (night) { if(monsters <= 0) { daytime = true; } } } }

Viewing all articles
Browse latest Browse all 184

Trending Articles



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