Question by
willtheboss8 · Sep 21, 2020 at 10:04 PM ·
instantiateloopingloops
trying to instantiate an object a random number of times ,how do i spawn something in a random ammount of times?
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;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
For and While loop crashing unity and system 1 Answer
How does one continuously move an object forward without using velocity? 1 Answer
i have a list of transforms and i want to spawn a prefab once for all the transforms in the list. 1 Answer
Nested For loop Grouping 0 Answers
GameObject destory. Questions and possibily solutions 0 Answers