- Home /
Question by
rodrigodebem7 · Jun 01, 2019 at 09:14 PM ·
unity 5visual studio
Instantiating an object only once within Update()
I'm trying to instantiate just one prefab inside the method udpate, but the thing is, it's instantiating twice. I don't know how I could solve this problem.
What could I do? I've created a boolean called spawned, but it's not working.
public class SpawnaCasos : MonoBehaviour
{
public GameObject[] Casos;
private GameController gameController;//variável para acessar o script do gamecontroller
private GameObject controller;
private bool spawned = false;
// Start is called before the first frame update
void Start()
{
controller = GameObject.FindGameObjectWithTag("Controller");
gameController = controller.GetComponent<GameController>();
}
// Update is called once per frame
void Update()
{
if (gameController.contadorPontos == 14 && spawned == false)
{
Instantiate(Casos[UnityEngine.Random.Range(0, 8)], transform.position, this.transform.rotation);
spawned = true;
}
}
}
Comment
Answer by instruct9r · Jun 01, 2019 at 11:15 PM
void Update()
{
if (gameController.contadorPontos == 14 && spawned == false)
{
spawned = true;
Instantiate(Casos[UnityEngine.Random.Range(0, 8)], transform.position, this.transform.rotation);
}
}
Move spawned above the Instantiate.
Answer by rodrigodebem7 · Jun 01, 2019 at 11:29 PM
Same thing man, it didn't work. I don't know why :(
I've discovered the issue man. There were two objects with the same script attached to ;/