Duplicate Question
Moving to Next Level on collision
hi I need help getting to the next level on my puzzle game, I want to get it so when it touches the exit it load the next level
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class LevelLoader : MonoBehaviour {
public string levelToLoad;
void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene("levelToLoad");
}
}
is there any other way i could do it or am i doing something wrong please help Thanks
At first, I think you have to check that does the collider collide anything by put this line inside the OnTriggerEnter before the Scene$$anonymous$$anager line: Debug.Log("Has collide " + other.name); I wonder that the IsTrigger check box of you game object are not checked yet, if not yet, check it . Hope this help
Answer by Eudaimonium · Jun 07, 2016 at 09:51 AM
You are declaring a variable called levelToLoad, but then in your function you're giving it a hardcoded string (as denoted by quotes), instead of actual variable.
Change your line to:
SceneManager.LoadScene(levelToLoad);
And make sure you add your scene to build order.
Open up the scene you wish to load next in your editor, so you can see it.
Open File -> Build Settings, and click "Add Open Scene".
You should now see both the scene you are coming from (assuming you added it), and the scene you wish to load in your build order.
Also, my advice is to use a scene index to load scenes, instead of a string representing the scene name.
no that did work you wouldn't know of another way to do it?
What do you mean by this?
Does it throw any error messages? Does the script correctly get called? Try putting a print("Collision"); statement before your Scene manager line, see if that gets printed in the console.
And no, Scene $$anonymous$$anager is the way to load new levels and scenes in your Unity game. Why do you need another way to do it?
have you write your next scene name to public variable levelToLoad in inspector where you attach your script?
Follow this Question
Related Questions
next level 1 Answer
Frogger Logs OnTriggerEnter2D OnTriggerExit2D 0 Answers
Move to the next level once all with "enemy" are dead with tag 2 Answers
Using OnTriggerStay for entering, staying or exiting 2 Answers
Use a string to call a class 2 Answers