- Home /
Player lives script
So i got a script that makes the player spawn and have lives everytime the player is destroyed by the enemy. The problem is the player will spawn in the beginning, but when an enemy destroys the player, the player gameobject does not respawn. Here is the script for reference, if you can help to fix this issue thank you. I don't know why the player does not respawn back after the enemy destroys the game object.
public Transform playerShip;
public int playerLives = 2;
public GameObject player;
void Start ()
{
if (player == null && playerLives >= 1) {
playerLives--;
Instantiate (playerShip, new Vector3 (0, 0, 0), Quaternion.Euler (0, 0, 180));
player = GameObject.FindGameObjectWithTag ("Player");
}
}
Do you load scene every time player destroys????
Answer by luther1321 · Aug 28, 2014 at 06:55 AM
You need to put that codeblock in void Update() It is only being issued 1 time when the gameObject this script is attached to is created.
So put this script on an empty gameobject somewhere in the world and you can call it something like "RespawnPoint"
public Transform playerShip;
public int playerLives = 2;
public GameObject player;
void Start ()
{
Instantiate (playerShip, gameObject.transform.position, Quaternion.identity);
player = GameObject.FindGameObjectWithTag ("Player");
}
void Update(){
if (player == null && playerLives >= 1) {
playerLives--;
Instantiate (playerShip, gameObject.transform.position, Quaternion.identity);
player = GameObject.FindGameObjectWithTag ("Player");
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I make my player re-spawn in its original place on collision of enemy? 1 Answer
A node in a childnode? 1 Answer
Stay Back! 2 Answers
Trap Door Question 1 Answer