How do I create a script to reset a level when my Player comes into contact with an object?
I am trying to find or get help creating a script to reset the level when my Player is "killed" or comes into contact with a certain object I have little coding knowledge so please if you would be so kind to dumb down your answers Thank You
I can't tell if you're asking about the unity API or a pattern to help you accomplish the task.
In general the unity script reference is a great resource for API stuff.
If you're asking about a pattern, I'd look into using the observer pattern. It will allow you to simply send out an event when an arbitrary action occurs. Other things 'observe' this event and react to it.
Using your example, you'd have a 'player died' event, and an observer that reacted to that by restarting the level. This kind of pattern makes it easy to change out what happens when the player dies by simply changing out what is observing the event.
Answer by itsharshdeep · Nov 25, 2015 at 04:38 AM
Hello
Can you please tell me If you re-load the scene then it will solve your problem or not? Reload the scene will act like same as you entered the scene. Now you have to do like suppose if your player have health. Now you have to place the check whether/if the player has health 0. Then you will show the game over screen & on "Retry" or "Play Again" button the scene must be reload. With the line Application.LoadLevel("SceneName");
or Application.LoadLevel(Application.loadedLevel);
Now that was the one answer to your question. For the Region / specific area when player get in that area. There are different types of option to do this . It is basically depend upon the scene. But the most common one is to place the empty triggered collider which will tell you when the player enter in the region.
For that you have to write like :-
void OnTriggerEnter(collider c){
if ( c.transform.tag == "Player"){
// Show game over screen here
}
}
void OnPlayAgainButton(){
Application.LoadLevel(Application.loadedlevel);
}
There is also one more solution of your question is to will track all the things of the objects, references in your game like player score, health , positions of the objects etc etc. and when the player get killed then you have to call the that Reset()
method in which it will reset all the vales. This method is little hard as compared to the above one. Note - Rest all the approach vary with your requirement of the games/requirements.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D not working 1 Answer
Detect the objects staying on top of the Particles 0 Answers
How can I remove movement or camera rotation in one direction with a trigger 0 Answers
Applying force when colliding with several objects 1 Answer
How to have a game object register its OnTriggerEnter function? 1 Answer