[英]My GameObject always stays inactive, SetActive(true) doesn't work and the coroutine won't start
[英]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.