I've designed a game where, when a button is clicked, the moving point stops. If the button is clicked again, the moving point starts moving again.
I only click on the button once, but the ToggleGame()
is always called twice. Does anyone know the reason?
private void Start()
{
if (toggleButton != null)
{
toggleButton.onClick.AddListener(ToggleGame);
}
StartGame();
}
public void StartGame()
{
isMoving = true;
// init some settings here
}
void Update()
{
if (isMoving)
{
// run the game
}
}
public void ToggleGame()
{
if (!isMoving)
{
StartGame();
UpdateButtonText("Stop");
}
else
{
isMoving = false;
CheckWinCondition();
UpdateButtonText("Start");
}
}
toggleButton.onClick.AddListener
and confirm the button no longer works (ensure not wired up elsewhere). How are you loading the scene (Any chance there are multiple instances of the GameObject live at the same time?). \$\endgroup\$