简体   繁体   English

协程不执行setActive(false)

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

I have an object in my game that is like a power object: when my player enters the power it should activate a panel that indicates that the power was grabbed, and past 3 seconds that panel should disappear. 我的游戏中有一个物体,就像一个力量物体:当玩家输入力量时,它应该激活一个面板,指示该力量已被抓住,并且在3秒钟后该面板应消失。 At the moment my panel appears when I hit the power, but it doesn't disappear. 此刻,当我按下电源时,面板会出现,但不会消失。 I am using a Coroutine like this: 我正在使用这样的协程:

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);
    }
}

Ps: what is inside the update is independent from what i need to do, so i think it doesn't interfer with my needs. 附言:更新中包含的内容与我需要执行的操作无关,因此我认为它不会干扰我的需求。

maybe you should check if your gameobject(shrink's) is unactive or destroyed. 也许您应该检查您的游戏对象(收缩)是否无效或被破坏。 coroutine does not work with unactive gameobject. 协程不适用于无效的游戏对象。

I think you might be destroying the GameObject before the WaitForSeconds(3f) has ended, which will prevent panel.SetActive(false) from executing. 我认为您可能在WaitForSeconds(3f)结束之前就销毁了panel.SetActive(false) ,这将阻止执行panel.SetActive(false)

In your Update-method's "else statement to if (temp.x > 0)" you're destroying the GameObject that tries to show/hide the panel. 在更新方法的“是否if(temp.x> 0)的其他语句”中,您正在破坏试图显示/隐藏面板的GameObject。

If you absolutely need to Destroy this gameobject at that time you should break out your IEnumerator to another script and call it from this (shrink.cs) script. 如果您当时绝对需要销毁此游戏对象,则应将IEnumerator分解为另一个脚本,然后从此(shrink.cs)脚本中调用它。

Destroying the object before 3 seconds might be the issue. 在3秒钟之前销毁对象可能是问题所在。 You can check by putting two logs: 您可以通过放置两个日志来检查:

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

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

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