0
\$\begingroup\$

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");
    }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Did you add the method as well over the editor? \$\endgroup\$
    – Zibelas
    Oct 18 at 7:00
  • 1
    \$\begingroup\$ As a debugging step... Comment out your 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\$
    – Basic
    Oct 18 at 11:09

1 Answer 1

2
\$\begingroup\$

This thread may help you. Here's a summary:

You may have your left-click set to 'submit' in input settings and it is both calling a submit and select event on your single-click of the toggle-button. The user in the linked thread reported solving the issue by removing 'mouse 0' from the 'submit' bind in input settings

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .