- Home /
Invoke not working,invoke not working
Hello, im trying to learn coding using this guide: https://www.youtube.com/watch?v=VbZ9_C4-Qbo
he uses invoke to have a delay between losing and restarting the game, but for some reason my code doesnt work. I cant seem to find the error and i dont get an error in the unity console.
This is my code:
using UnityEngine; using UnityEngine.SceneManagement;
public class Gamemanager : MonoBehaviour {
bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel()
{
completeLevelUI.SetActive(true);
}
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
} ,Hello, im currently trying to learn to code in unity. I am using this guide:https://www.youtube.com/watch?v=VbZ9_C4-Qbo
and he uses invoke as a method to have a delay between losing the game, and the game restarting. But the invoke doesnt work for me. I am sure that i have written it right, but you never know.
This is my code:
using UnityEngine; using UnityEngine.SceneManagement;
public class Gamemanager : MonoBehaviour {
bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel()
{
completeLevelUI.SetActive(true);
}
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
tiny protip: write nameof(Restart)
ins$$anonymous$$d of "Restart"
.
I.e.: Invoke( nameof(Restart) , restartDelay );
Try this, so you know if Restart
is being called and what scene is being actually loaded:
void Restart ()
{
string activeSceneName = Scene$$anonymous$$anager.GetActiveScene().name;
Debug.Log($"{nameof(Restart)}() called. Loading '{activeSceneName }' now");
Scene$$anonymous$$anager.LoadScene( activeSceneName );
}
Answer by Nascapit · Jul 16, 2020 at 09:57 PM
Hi, I have not used invoking methods by it's name before. According to Unity documentation, it's better to use coroutines for something like this. (The documentation might be confusing so you might wanna look up a video on this)
https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity3.html
With coroutines you can stop the execution and resume it later.
Your code with a coroutine would look like this:
public IEnumerator Restart(float f)
{
yield return new WaitForSeconds(f);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
and then the call:
StartCoroutine(Restart(restartDelay));
Answer by huseyinbaba58 · Jul 16, 2020 at 08:18 PM
You should use StartCoroutine(IEnumerator stuff).
Your answer
Follow this Question
Related Questions
Is it possible to change Invoke Repeating rate on the fly? 3 Answers
What is the difference between InvokeRepeating & Invoke and how can they be used? 1 Answer
[SOLVED]Destroy gameObject, not OnCollisionEnter but after a while 1 Answer
Making Enemy Retreat 1 Answer
Issue with starting/canceling invokes 0 Answers