简体   繁体   English

在Update()中仅对对象进行一次实例化

[英]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. 我试图在方法udpate中只实例化一个预制件,但事实是,它实例化了两次。 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. 我创建了一个名为spawned的布尔值,但它不起作用。

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;
        }
    }
}
public class SpawnaCasos : MonoBehaviour
{    
    public GameObject[] Casos;

    private GameController gameController;
    private GameObject controller;

    void Start()
    {
        controller = GameObject.FindGameObjectWithTag("Controller");
        gameController = controller.GetComponent<GameController>();
        InstantiateCasos();
    }

    void Update()
    {
        if (gameController.contadorPontos == 14)
            InstantiateCasos();
    }
    void InstantiateCasos(){
        Instantiate(Casos[UnityEngine.Random.Range(0, 8)], transform.position, transform.rotation);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM