单击预制件时如何销毁预制件?

时间:2019-10-28 18:14:07

标签: c# unity3d

我有以下代码,其中不同类型的水果会在屏幕的右侧生成,然后移至左侧,它们消失:

    public float tiempoRespawn = 1.0f;
    private Vector2 limitesPantalla;
    public Button botonJugar;

    void Start()
    {
        limitesPantalla = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
        botonJugar.onClick.AddListener(ButtonJugarClicked);
    }

    void ButtonJugarClicked()
    {
        StartCoroutine(frutas());
        botonJugar.interactable = false;
    }

    private void spawn(){
        GameObject f = Instantiate(frutaPrefab[Random.Range(0, frutaPrefab.Length)]) as GameObject;
        f.transform.position = new Vector2(-limitesPantalla.x * -2, Random.Range(-limitesPantalla.y, limitesPantalla.y));
    }

    IEnumerator frutas()
    {
        for (int i = 0; i <= 10; i++)
        {
            yield return new WaitForSeconds(tiempoRespawn);
            spawn();
            if (i == 10){
                botonJugar.interactable = true;
            }
        }
    }

我想在水果到达屏幕左侧之前单击它们,以销毁它们。这是预制的代码,如果有帮助的话:

    private Rigidbody2D rb;
    private Vector2 limitesPantalla;

    void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
        rb.velocity = new Vector2(velocidad, 0);
        limitesPantalla = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    }


    void Update()
    {
        if(transform.position.x > -limitesPantalla.x)
        {
            Destroy(this.gameObject);
        }
    }

0 个答案:

没有答案