问题
I started to learn coding myself by watching videos, reading web articles etc. and i thought that learning by doing would suit better for me so i started to make a game with Unity through Brackeys tutorial videos.
using UnityEngine.SceneManagement;
using UnityEngine;
public class gameManager : MonoBehaviour
{
bool hasGameEnded = false;
public float restartDelay = 1f;
public void endGame()
{
if (hasGameEnded == false)
{
hasGameEnded = true;
Invoke("Restart", 2f);
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
So my problem here is when i use invoke with the method Restart none of the callings works. "Warning CS8321 The local function 'Restart' is declared but never used" Thats the error i get.
If wanted i can show where and how i used the endGame method. Thanks for any kind of help.
回答1:
If the Void
Was inside another Void
it wont work.
So you need to change :
using UnityEngine.SceneManagement;
using UnityEngine;
public class gameManager : MonoBehaviour
{
bool hasGameEnded = false;
public float restartDelay = 1f;
public void endGame()
{
if (hasGameEnded == false)
{
hasGameEnded = true;
Invoke("Restart", 2f);
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
To :
using UnityEngine.SceneManagement;
using UnityEngine;
public class gameManager : MonoBehaviour
{
bool hasGameEnded = false;
public float restartDelay = 1f;
public void endGame()
{
if (hasGameEnded == false)
{
hasGameEnded = true;
Invoke("Restart", 2f);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
来源:https://stackoverflow.com/questions/65526960/when-i-use-invoke-to-a-method-i-want-to-use-i-cant-call-it-anymore