3

I have two scripts, one of them restarts the scene and the other is a countdown timer than calls the restart scene method in the first script. However, it's not restarting and I don't understand why even though there are no errors.

The first script that restarts the scene:

using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelComplete : MonoBehaviour
{
    public void LoadNextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void Exit()
    {
        Application.Quit();
        Debug.Log("Exit");
    }

    public void Restart()
    {
        SceneManager.LoadScene(sceneBuildIndex: 1);
        Debug.Log("restart pressed");
    }
} 

The second script that's supposed to restart the scene after a countdown timer ends:

using UnityEngine;
using UnityEngine.UI;

public class TimerCounDown : MonoBehaviour {

    [SerializeField] private Text uiText;

    [SerializeField] private float MainTimer;

    private float timer;
    private string canCount;
    private bool doneOnece;
    public float restartDelay = 5f;
    private string methName;

    private void Update()
    {
        timer -= Time.deltaTime; 
        Debug.Log((MainTimer - (-timer)));

        if ((MainTimer - (-timer)) >0)
        {
            canCount = (MainTimer - (-timer)).ToString("F1") + " Seconds until end";
            uiText.text = canCount;
        }
        else
        {
            uiText.text = "level complete lefel will be restarted in 5 seconds";
            // GetComponent<LevelComplete>();
            // Invoke("Restart", restartDelay);
            // GetComponent<LevelComplete>().Restart();
        }
    }
}

I am trying to restart it with Invoke but it can't take GetComponent<LevelComplete>().Restart() as parameter so I decided to just simply fire this method and it doesn't work. I don't understand why and how to fix it. Please help me if you know where the problem is and the solution to it.

1 Answer 1

9

Invoke is a method belonging to a MonoBehaviour instance.

When you call Invoke("Restart", restartDelay); directly, the runtime will try and find a method called "Restart" within the TimerCountDown class since its where you called Invoke from, which doesn't exist. This explains why nothing happens.

The correct way would be to first reference the LevelComplete instance and then using that to call Invoke:

LevelComplete levelComplete = GetComponent<LevelComplete>();
levelComplete.Invoke("Restart", restartDelay);

Which will correctly look for the "Restart" method within the LevelComplete class.

Sign up to request clarification or add additional context in comments.

5 Comments

actualy its throwing this error NullReferenceException: Object reference not set to an instance of an object TimerCounDown.Update () (at Assets/Scripts/TimerCounDown.cs:32)
line 32 right now is levelComplete.Invoke("Restart", restartDelay);
That would mean GetComponent<LevelComplete>() returned null. Are your LevelComplete and TimerCountDown scripts attached to the same object?
script connected to the UI text object in script parameter UIText taking same object so it can show timer and message
thanks I attached it to timer object and it start working :) Thank you !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.