- Home /
My player magically disappears after level restart.
I have a flappy bird type game, it works all fine. But when my player dies and the level restarts, the player is not on the screen anymore.
code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
// force to add
public Vector2 Force = new Vector2(0,300);
// Update is called once per frame
void Update ()
{
// if the player hits the space key
if(Input.GetKeyUp (KeyCode.Space))
{
// set velocity to zero
rigidbody2D.velocity = Vector2.zero;
// add force
rigidbody2D.AddForce (Force);
}
// get screen position
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
// if the screen position is more than screen height or less, restart level
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
respawn();
}
}
// restart level when player dies
void OnCollisionEnter2D (Collision2D col)
{
respawn ();
}
void respawn()
{
Application.LoadLevel (Application.loadedLevel);
}
}
pictures
before restart
after restart
Where do you set the start position of the plane?
Odd. Items with static or DontDestroyOnLoad don't reset. Any chance this is part of the problem?
Anther possibility is if you are moving your player based on Time.time. You should use Time.timeSinceLevelLoad ins$$anonymous$$d.
Answer by AcE_fLoOdEr · Nov 04, 2014 at 02:24 AM
I figured it out. Apparently, my character goes behind the background on restart. So all I had to do was set it's position on the Z axis to -1 before playing the game. It works fine now, thanks for the help tho <3
Answer by ExtremePowers · Nov 02, 2014 at 09:35 PM
You need to set the DontDestroyOnLoad(this.gameObject); Then you should just be able to set the position of the gameObject to the start position.
This could work. But reloading the scene should reset everything back to its original position in the scene.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player lives script help 1 Answer
Should I go with C# and Unity or C++ and Unreal/Some other engine 0 Answers
Changing Axis to Buttons 2 Answers
UDP implementation in Unity, unable to send to two different machines 0 Answers