- Home /
Respawn Script Problem
The problem with the below code is that it only works once and I want it to work every time the ship dies.
I have a health script on my ship that has a the fallowing code for the resawn. This script is longer but this is the part that does it. destroyed is called.
void destroyed()
{
Destroy (GameObject.Find("Guns"),1);
animation.wrapMode = WrapMode.Once;
animation.wrapMode = WrapMode.Once;
animation["LeftWing"].wrapMode = WrapMode.Once;
animation["LeftWing"].layer = 3;
animation.Play ("LeftWing");
Destroy (GameObject.Find("Left_Wing"),3);
animation.wrapMode = WrapMode.Once;
animation["RightWing"].wrapMode = WrapMode.Once;
animation["RightWing"].layer = 4;
animation.Play ("RightWing");
Destroy (GameObject.Find("Right_Wing"),3);
animation.wrapMode = WrapMode.Once;
animation["RightThruster"].wrapMode = WrapMode.Once;
animation["RightThruster"].layer = 5;
animation.Play ("RightThruster");
Destroy (GameObject.Find("Right_Engine"),3);
animation.wrapMode = WrapMode.Once;
animation["LeftThruster"].wrapMode = WrapMode.Once;
animation["LeftThruster"].layer = 6;
animation.Play ("LeftThruster");
Destroy (GameObject.Find("Left_Engine"),3);
animation.wrapMode = WrapMode.Once;
animation["WindowBlowoff"].wrapMode = WrapMode.Once;
animation["WindowBlowoff"].layer = 7;
animation.Play ("WindowBlowoff");
Destroy (GameObject.Find("Window"),3);
animation.wrapMode = WrapMode.Once;
animation["WindowFrameBlowoff"].wrapMode = WrapMode.Once;
animation["WindowFrameBlowoff"].layer = 8;
animation.Play ("WindowFrameBlowoff");
Destroy (GameObject.Find("Window_Frame"),3);
WingDamage.SetActive(false);
WingDamage2.SetActive(false);
Destroy (GameObject.Find("Star Blaster"),4);
Destroy (GameObject.Find ("Star Blaster(Clone)"),4);
GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
varGameObject.GetComponent<CameraFollow>().enabled = false; // Finds the fly script and turns it off if we die
curShipHealth = 0;
texture.GetComponent<AddDamage>().thirdDamage = true; // Finds the fly script and turns it off if we die
MassDamage.SetActive(true);
explosion.SetActive(true);
player.GetComponent<ShipThrusterControle>().level3 = true; // Finds the fly script and turns it off if we die
// texture.GetComponent<AddDamage>().firstDamage = false; // Finds the fly script and turns it off if we die
// texture.GetComponent<AddDamage>().secondDamage = false; // Finds the fly script and turns it off if we die
// texture.GetComponent<AddDamage>().thirdDamage = false; // Finds the fly script and turns it off if we die
// texture.GetComponent<AddDamage>().Dead = false; // Finds the fly script and turns it off if we die
//
// texture.GetComponent<AddDamage>().Repair = true; // Finds the fly script and turns it off if we die
// texture.GetComponent<AddDamage>().Repair = false; // Finds the fly script and turns it off if we die
curShipHealth = 100;
StartCoroutine(Respawn());
}
IEnumerator Respawn()
{
yield return new WaitForSeconds (3);
myShip.SetActive (false);
GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
Spawn respawn = varGameObject.GetComponent<Spawn>(); // Finds the fly script and turns it off if we die
respawn.enabled = true;
respawn.dead = true;
}
This turns on a script I have on my main camera that I am trying to use to respawn the ship.
This code:
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour
{
public Transform ship;
public Transform location1; // The teleport after death location
private Transform newShip;
public bool dead = false;
void Start ()
{
Dead();
}
void Dead()
{
if(dead)
{
GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
newShip = GameObject.FindWithTag("Player").transform;
GameObject varGameObject = GameObject.Find("Main Camera");
CameraFollow cameraFollow = varGameObject.GetComponent<CameraFollow>();
ShipHealth health = varGameObject.GetComponent<ShipHealth>();
cameraFollow.enabled = true;
cameraFollow.target = newShip;
dead = false;
varGameObject.GetComponent<Spawn>().enabled = false; // Finds the fly script and turns it off if we die
}
}
}
Answer by Tomer-Barkan · Nov 18, 2013 at 06:36 PM
You're calling Dead() in your camera only once, in the Start() method. Try using Update() instead of Start(), so it will check the dead
variable every frame and not just once:
void Update() {
Dead();
}
I have been playing with that this is what the new script on the camera looks like and its works the same :
using UnityEngine;
using System.Collections;
public class Spawn : $$anonymous$$onoBehaviour
{
public Transform ship;
public Transform location1; // The teleport after death location
private Transform newShip;
public bool dead = false;
void Update()
{
if(dead)
{
GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
newShip = GameObject.FindWithTag("Player").transform;
GameObject varGameObject = GameObject.Find("$$anonymous$$ain Camera");
CameraFollow cameraFollow = varGameObject.GetComponent<CameraFollow>();
ShipHealth health = varGameObject.GetComponent<ShipHealth>();
cameraFollow.enabled = true;
cameraFollow.target = newShip;
dead = false;
}
}
}
You got a lot of logic in your code, and something might be messing things up... Start by putting a Debug.Log just before calling StartCoroutine()
, one before and one after the yield statement. Also add another Debug.Log inside the if(dead)
statement. Check out which of them are being printed.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to undo destroying a component, after 5 seconds. 0 Answers
Distribute terrain in zones 3 Answers
With my respawn script, Player can't move for about 5 seconds. 0 Answers