How do I get my character to go the the next level?
Hi, so I have the script in player that checks to see if the player collides with the exit, but it doesn't actually load the new map after doing so.
private void OnTriggerEnter2D (Collider2D other)
{
//Check if the tag of the trigger collided with is Exit.
if(other.tag == "Exit")
{
//Invoke the Restart function to start the next level with a delay of restartLevelDelay (default 1 second).
Invoke ("Restart", restartLevelDelay);
//Disable the player object since level is over.
enabled = false;
}
........
private void Restart ()
{
//Load the last scene loaded, in this case Main, the only scene in the game.
Application.LoadLevel (Application.loadedLevel);
}
I am just not understanding why it isn't loading the new level. If it helps, I have basically combined the code for the unity 2d procedural dungeon generation and the 2d rogue-like tutorials. Any help or suggestions would be appreciated.
And if there are any clarifications that need to be made please let me know.
So, there are a number of possible failure points. You need to figure out which one is the cause of your problem.
Do you ever get inside the OnTriggerEnter2D method?
Do you get inside the "if (other.tag == "Exit")" block?
Is the "Restart()" method being called?
Add a Debug.Log statement in each of those areas and see which one you don't see in the console.
If you don't find the problem, report back the results for some more assistance.
@jgodfrey thank you for that. I just checked, but I had all three messages output (one in the on trigger, one in the if statement, and one in the restart() method). It just goes to a screen that says the scene is missing a fullscreen camera, but the camera is attached to the player.
Hmmm... I think the code you're using in Restart() is deprecated, but I'd think it'd work. You could try this ins$$anonymous$$d (which should be the more current way to do the same thing).
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex);
... And, you'll need to add a
using UnityEngine.Scene$$anonymous$$anagement;
to the top .
Answer by Ali-hatem · Apr 19, 2016 at 01:59 PM
@Dugat you are disabling the player object that holds this script before 1 second of calling the restart so there will be no script & no reload & what you have to do is don't disable the object since it will be loaded again or move enabled = false;
to Restart ()
@jgodfrey i thought you suspected that when you asked for debug
Is the "Restart()" method being called?
but i think he didn't put it in Restart () .
Hmm.... Yeah, you're right. I have my head in too many things... ;^). Anyway, according to the reply, a Debug.Log was added in Restart and the message was output to the console...
I wonder if the disabled player isn't being re-enabled when the scene is restarted...
Your answer
Follow this Question
Related Questions
Walking around 2D planets? 1 Answer
2D drag and throw ragdoll physics 0 Answers
How do I assign a velocity to an object on only 1 axis? 1 Answer
Is there any way to prevent Polygon collider auto generation in Unity 5.3? 0 Answers
2D Accuracy 0 Answers