我的代码:调用()不会被调用
public class Enemy : MonoBehaviour
{
public GameObject deathEffect;
public float health = 4f;
public static int EnemiesAlive = 0;
public int loadToScene;
void Start()
{
EnemiesAlive++;
}
void OnCollisionEnter2D (Collision2D colInfo)
{
if (colInfo.relativeVelocity.magnitude > health)
{
Die();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
EnemiesAlive--;
if (EnemiesAlive <= 0)
{
Debug.Log ("LEVEL WON!");
Invoke("ChangeScene",2f);
}
Destroy(gameObject);
}
void ChangeScene()
{
Debug.Log ("Hello");
loadToScene = 1;
SceneManager.LoadScene (loadToScene);
}
}
任何人都能指出我为什么Invoke()
不起作用?一旦满足条件(EnemiesAlive <= 0)
,我想在改变场景之前增加2秒的延迟时间。输出Debug.Log ("LEVEL WON!");
,而Debug.Log ("Hello");
不输出。我知道我也可以使用StartCoroutine
,但我想用Invoke()
代替。
谢谢你正确预测我的意图。我之前忘了提及。我已成功按照步骤操作,“添加下面的脚本...”,但我不确定如何将给定的代码添加到我的代码中。我试过并在SceneLoader上出现错误。我需要首先声明SceneLoader吗?我应该声明哪种类型? – jer
@jer将指令的最后部分直接修改到您的'Enemy'脚本中。只需复制一下。只要你创建了SceneLoader脚本并将其附加到名为*“SceneLdObj”*的新GameObject中。这应该工作。 – Programmer
我只是最近使用stackoverflow.com。我不知道我必须接受。对不起。感谢您的高举。 :) – jer