Can someone tell me how to restart the scene if my player hits two objects
So I have a game where a 2d spaceship is flying through a cave and I need it so that once I hit an object the level restarts It would be great if someone could tell me how to do this. thanks.
Answer by VeeooshL · Apr 06, 2017 at 02:07 PM
here:
void OnCollisionEnter2D (Collision2D other) {
if(other.gameObject.tag == "Player")
{
Application.LoadLevel(Application.loadedLevel);
}
}
put this on your object that gets hit. on its collider2d, make sure the 'Trigger' checkbox is ticked. also make sure your player has the tag 'Player'. here is how you do that in the inspector tab:
the object that needs to collide is called ground would i put void OnCollisionEnter2D (Collision2d ground) etc
no, as you put this script on the ground and the name 'other' is simply a placeholder, used to represent the player.
Answer by UnityCoach · Apr 06, 2017 at 02:20 PM
You can use SceneLoader.LoadScene () to simply reload the level, though, performance wise, I would advise to simply reset objects and data to their original states.
store/reset objects' transforms and states
destroy every object that was instantiated, or deactivate every pooled object that were activated
I can suggest that you have a look at my full training course, in which I cover the subject, along with many other things.