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.