Search code examples
c#unity-game-enginecanvaspanelcoroutine

Unity 2018 script to make panel scroll up wont work if I go to the game scene before the credits scene


I am working on a game in Unity3D (what else would I be making) and so far I have the Main menu scene, the game scene and the Credits scene.

There is a script I made (shown below) That will make the panel holding the names scroll up that works fine if I select the credits scene from the main menu. But here is the problem. If I go to the game first and then go back to the main menu rand select credits nothing happens. Any ideas?

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

Solution

  • You moved your Translate inside the Start(), it won't work that way. Only StartCoroutine should be in Start(), like this :

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

    Note : I changed LoadLevel to SceneManager.LoadScene because it's deprecated, and will be removed in the future.