0
\$\begingroup\$

I've made some buttons on my screen to navigate to other scenes of my Unity project but the Click(string command) method doesn't trigger. I've placed a breakpoint on the bold line in code below, but the pointer never hits that point.

public class GameNavigation : MonoBehaviour
{
    public void Click(string command)
    {
        switch (command)
        {
            case "again":
                Application.LoadLevel(Application.loadedLevel);
                break;

            case "go_to_menu":
                Application.LoadLevel("Navigation");
                break;

            default:
                break;
        }
    }
}

Here are also some screenshots form my Unity project.

Inspector tools for navigation, btn again and event system and hierarchy

What did I wrong?


Note one: I've made also this code that is called after the scene ends. the parameter command is always an empty string.

public void RestartLevel(string command)
{
    switch (command)
    {
        case "again":
            Application.LoadLevel(Application.loadedLevel);
            break;

        case "go_to_menu":
            Application.LoadLevel("Navigation");
            break;

        default:
            break;
    }
}

If I place it in a comment, I get this error:

'Player' AnimationEvent 'RestartLevel' has no receiver! Are you missing a component?


Note two: I also use a mouse down event for when the player must shoot.


Update one: by a comment below I've made this code:

public Button btnAgain;
public Button btnMenu;

public void Start()
{
    btnAgain.onClick.AddListener(() => {
        Application.LoadLevel(Application.loadedLevel);
    });

    btnMenu.onClick.AddListener(() => {
        Application.LoadLevel("Navigation");
    });
}

The Start() method is always called, no problems with it. But the Application.LoadLevel(...) code never executes when I click on the methods. However btnMenu and btnAgain aren't null.


Update two: I've upload a zip file on my SharePoint that you can access. Start a game and choose level 01. You navigating to a new scene. On that scene you have on the right below side you have two buttons, these are the buttons that doesn't work. If you can find the problem, please give me the solving of the problem. Other bug fixes are also welcome. 😉

\$\endgroup\$
  • \$\begingroup\$ For clicking event you will have to write "ButtonNam.onClick.AddListener". \$\endgroup\$ – Shraddha Patel Jan 4 '16 at 11:42
  • \$\begingroup\$ Go through this and try once answers.unity3d.com/questions/938496/… \$\endgroup\$ – Shraddha Patel Jan 4 '16 at 11:44
  • 2
    \$\begingroup\$ First check that the method is actually being called and with what parameter. Second, check that strings aren't immutable (which makes == not a valid comparison and you have to use .equals()). Third, if == is valid, it still may not work correctly in a switch statement. 4th, change your code to use enums so it isn't stringly typed. \$\endgroup\$ – Draco18s Jan 4 '16 at 16:00
  • \$\begingroup\$ @Draco18s: The RestartLevel is always called after the last animation (this is the game over animation when the player is dead). the parameter command is in that case an empty string. \$\endgroup\$ – H. Pauwelyn Jan 4 '16 at 16:05
  • \$\begingroup\$ If you're passing an empty string, then you've intentionally constructed your method to do nothing. \$\endgroup\$ – Draco18s Jan 4 '16 at 16:08
1
\$\begingroup\$

Here's the process to figuring out what's causing the issue.

  1. Confirm that the Clicked method is run. I suggest just putting a Debug.Log() statement there. Debug.Log(command); would be perfect, because it also lets you know the command.
  2. If your Click method isn't being run, most likely something else is taking that click. Look for other objects that might be intercepting the click in front of it (Might not be visible).
  3. If the correct command is received, then you should probably use .equals() for comparison. For instance, command.equals("again")
  4. You might consider using separate functions for each. There's a proposal to use enums, but so far as I know, that's not accepted yet.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.