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.
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. 😉
==
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\$RestartLevel
is always called after the last animation (this is the game over animation when the player is dead). the parametercommand
is in that case an empty string. \$\endgroup\$