我正在使用Unity3D开发一款游戏(我还要做些什么),到目前为止,我拥有主菜单场景,游戏场景和Credits场景。
我编写了一个脚本(如下所示),该脚本将使包含名称的面板向上滚动,如果我从主菜单中选择了信用场景,则该脚本可以正常工作。但这是问题所在。如果我先去玩游戏,然后再回到主菜单rand select credits,则什么也没有发生。有什么想法吗?
using UnityEngine;
using System.Collections;
public class ScrollCredits : MonoBehaviour
{
public GameObject Canvas;
public int speed = 1;
public string level;
private void Start()
{
Canvas.transform.Translate(Vector3.up * Time.deltaTime * speed);
StartCoroutine(waitFor());
}
private void Update()
{
}
IEnumerator waitFor()
{
yield return new WaitForSeconds (69);
Application.LoadLevel(level);
}
}
答案 0 :(得分:0)
您将“翻译”移到了Start()
内,这样无法正常工作。
只能将StartCoroutine
放在Start()
中,如下所示:
public GameObject canvas;
public float speed = 0.1f;
public string sceneName;
public float timer;
private void Start()
{
StartCoroutine(WaitFor());
}
private void Update()
{
canvas.transform.Translate(Vector3.right * Time.deltaTime * speed);
}
IEnumerator WaitFor()
{
yield return new WaitForSeconds (timer);
SceneManager.LoadScene(sceneName);
}
注意:我已将LoadLevel
更改为SceneManager.LoadScene
,因为它已被弃用,以后将被删除。