如果我在功劳场景之前进入游戏场景,使面板向上滚动的Unity 2018脚本将无法工作

时间:2019-07-08 19:12:16

标签: c# unity3d canvas panel coroutine

我正在使用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);
}
}

1 个答案:

答案 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,因为它已被弃用,以后将被删除。