I'm trying to make it so when my player gets five points and they collide with the door, the menu screen for my game shows up. I thought this code was fine but the main menu won't show when I collide with the door. Can anyone help me fix this? Here is my code below:
private void OnTriggerEnter(Collider door)
{
if(door.tag == "Door" && points == 5)
{
SceneManager.LoadScene("MainMenu");
}
}
}
Im also using unityengine.scenemanagement too. The rest of the code including the points works fine it's just the loading of the scene that won't work.
CodePudding user response:
There are four things happening here, so you should check all of them in order to find your problem.
- Put a
Debug.Log()
inOnTriggerEnter
to make sure that it's actually being called - Print out
door.tag
to make sure that it matches "Door." Note that it's case sensitive. - Print out
points
to make sure that the value matches 5. - Double check that your scene name matches "MainMenu." (EDIT: not case sensitive) You can also load the scene by build index so that there's no risk of mismatching magic strings.
SceneManager.LoadScene (/* your scene index */);
Or
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex 1);
Scene indexes begin at 0 and count up according to the order the scenes are arranged in your build settings. That can be found in File > Build Settings
CodePudding user response:
Some scenes have not been loaded into the build settings, so when jumping from the current scene to other scenes, other scenes cannot be loaded. You must load all the scenes you want to use into the scenes in buding list in the build settings. Method: After loading a scene, click the build settings option of the FILE menu, open the build settings menu, and then click the "add current" button in the menu. You can see that the current scenes are added to the build settings menu list. Open other scenes in this way and add them to the scenes in buding list in the uild settings menu. After adding all the scenes, save the project and run it again, I hope it can help you