Subtraction of lives not happening and no errors

I have the fallowing script I am using to respawn my ship when it gets destroyed but the problem is with the line that is subtracting from the lives its not doing it.

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour 
{
	public Transform ship;
	public bool death1 = false;
	public bool death2 = false;
	public Transform location1; // The teleport after death location
	private Transform newShip;
	
	void Start ()
	{
		if(death1)
		{
			GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
			newShip = GameObject.FindWithTag("Player").transform;
			
		GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
		 	varGameObject.GetComponent<CameraFollow>().enabled = true; // Finds the fly script and turns it off if we die
		   varGameObject.GetComponent<CameraFollow>().target = newShip; // Finds the fly script and turns it off if we die
			varGameObject.GetComponent<ShipHealth>().lives --;
		  // varGameObject.GetComponent<Spawn>().enabled = false; // Finds the fly script and turns it off if we die
			
			
		}
		if(death2)
		{
			GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
			newShip = GameObject.FindWithTag("Player").transform;
			
		GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
		 	varGameObject.GetComponent<CameraFollow>().enabled = true; // Finds the fly script and turns it off if we die
		   varGameObject.GetComponent<CameraFollow>().target = newShip; // Finds the fly script and turns it off if we die
			varGameObject.GetComponent<ShipHealth>().lives --;
		 //  varGameObject.GetComponent<Spawn>().enabled = false; // Finds the fly script and turns it off if we die
			
			
		}
		
	}
}

1 Answer

1

try(also keep references to your components if you can).

using UnityEngine;
using System.Collections;
 
public class Spawn : MonoBehaviour 
{
    public Transform ship;
    public bool death1 = false;
    public bool death2 = false;
    public Transform location1; // The teleport after death location
    private Transform newShip;
 
    void Start ()
    {
       if(death1)
       {
		GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
		newShip = GameObject.FindWithTag("Player").transform;
 
		GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
		CameraFollow cameraFollow = varGameObject.GetComponent<CameraFollow();
		ShipHealth health =  varGameObject.GetComponent<ShipHealth>();

		cameraFollow.enabled = true; // Finds the fly script and turns it off if we die
		cameraFollow.target = newShip; // Finds the fly script and turns it off if we die
		health.lives -= 1;
         // varGameObject.GetComponent<Spawn>().enabled = false; // Finds the fly script and turns it off if we die
 
 
       }
       if(death2)
       {
         GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
         newShip = GameObject.FindWithTag("Player").transform;
 
		CameraFollow cameraFollow = varGameObject.GetComponent<CameraFollow();
		ShipHealth health =  varGameObject.GetComponent<ShipHealth>();

		cameraFollow.enabled = true; // Finds the fly script and turns it off if we die
		cameraFollow.target = newShip; // Finds the fly script and turns it off if we die
		health.lives -= 1;

        //  varGameObject.GetComponent<Spawn>().enabled = false; // Finds the fly script and turns it off if we die
 
 
       }
 
    }
}