繁体   English   中英

协程不执行setActive(false)

[英]Coroutine doesn't execute setActive(false)

我的游戏中有一个物体,就像一个力量物体:当玩家输入力量时,它应该激活一个面板,指示该力量已被抓住,并且在3秒钟后该面板应消失。 此刻,当我按下电源时,面板会出现,但不会消失。 我正在使用这样的协程:

using UnityEngine;
using System.Collections;

public class shrink : MonoBehaviour {

    public float value = 0.1f; //1 by default in inspector
    private bool colided = false;
    private float speed;
    Manager gameManager;
    public GameObject panel;
    // Update is called once per frame
    void Start(){
        speed = 3.4f;
        gameManager = GameObject.Find ("GameController").GetComponent<Manager> ();
    }

    void OnTriggerEnter(Collider c)
    {
        if (c.gameObject.tag == "Player") {
            colided = true;
            gameManager.powerUp1 = true;
            StartCoroutine(menuOp());
        }
    }

    //This method is executed every frame
    void Update(){
        if (colided) {
            Vector3 temp = transform.localScale; 
            //We change the values for this saved variable (not actual transform scale)
            temp.x -= value * Time.time;
            temp.y -= value * Time.time;

            if (temp.x > 0) {
                speed += 0.02f;
                transform.Rotate (0f, 0f, Time.deltaTime * 90 * speed);
                transform.localScale = temp;
            } else {
                Object.Destroy (this.gameObject);
            }
        }
    }


    IEnumerator menuOp(){
        panel.SetActive (true);
        yield return new WaitForSeconds (3f);
        panel.SetActive (false);
    }
}

附言:更新中包含的内容与我需要执行的操作无关,因此我认为它不会干扰我的需求。

也许您应该检查您的游戏对象(收缩)是否无效或被破坏。 协程不适用于无效的游戏对象。

我认为您可能在WaitForSeconds(3f)结束之前就销毁了panel.SetActive(false) ,这将阻止执行panel.SetActive(false)

在更新方法的“是否if(temp.x> 0)的其他语句”中,您正在破坏试图显示/隐藏面板的GameObject。

如果您当时绝对需要销毁此游戏对象,则应将IEnumerator分解为另一个脚本,然后从此(shrink.cs)脚本中调用它。

在3秒钟之前销毁对象可能是问题所在。 您可以通过放置两个日志来检查:

1-在“ Object.Destroy”行之后2-在“ WaitForSeconds”之后

暂无
暂无

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

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