How do I switch scenes when player dies

I had made out an apply damage script . The problem I am having is the when the player health gets low the scene does not change . Here is my script :

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Collider))]
public class ApplyDamage : MonoBehaviour {

	//Drag this class on the object you want to modify player's health!

	//This handles subtracting and adding health points to player in a convenient way.
	/*As follows:
	 * Tick 'Is this healing player' true or false in the inspector (consequently it will add or subtract health points)
	 * Declare amount (on a 1000 scale not on 100!)
	 * Add a collider or trigger collider depending on your needs. See below the proper sections for different
	 * behaviors!
	 * */


	public bool IsThisHealingPlayer;
	public bool IsThisPickupable;
	public int AmountOfHealth;
	public bool instantDeath;

	int health;
	Healthbar healthbar;

	void Start(){

		healthbar = FindObjectOfType<Healthbar>();
		if (healthbar == null) {
			Debug.LogError("Healthbar class is not found in scene!");
		}

		if (IsThisHealingPlayer) 
			health = AmountOfHealth * -1;
		else
			health = AmountOfHealth * 1;

	}

	//Trigger collider:
	//For zone damage (runs every frame and damages/heals player when inside trigger zone (eg. fire, radiation field, etc)
	void OnTriggerStay(Collider other) {
		if (other.transform.tag == "Player" && !IsThisPickupable) {
			healthbar.SendMessage("ModifyHealth", health, SendMessageOptions.DontRequireReceiver);
		}
	}

	//Trigger collider:
	//For picking up medicine boxes or health potions
	void OnTriggerEnter(Collider other){
		if (other.transform.tag == "Player") {
			if (instantDeath) {
				healthbar.health = 0;
			}
			if (IsThisPickupable && healthbar.health < 1000) {
				healthbar.health = healthbar.health - health;
				healthbar.health = Mathf.Clamp(healthbar.health, 0, 1000);
				Destroy(gameObject);
				SceneManager.LoadScene("T");
			}
		}
	}	

	//Plain collider (eg. sphere for bullet)
	//This is to damage player on impact (rock falling, bullet hitting, etc)
	//Note: Check collision matrix for ControllerColliderHit, also, colliding with character controller is a bit more
	//complex so modify this section according to your needs.
	void OnControllerColliderHit(ControllerColliderHit other) {
		if (other.transform.tag == "Player") {
			healthbar.SendMessage("ModifyHealth", health, SendMessageOptions.DontRequireReceiver);
		}
	}

	/* This is to be used if the player is a rigidbody (eg. rollerball)
	void OnCollisionEnter(Collision other) {
		if (other.transform.tag == "Player") {
			healthbar.SendMessage("ModifyHealth", health, SendMessageOptions.DontRequireReceiver);
		}
	}*/
}

Tip 1: The following line can be edited.

...
Destroy(gameObject);
SceneManager.LoadScene("T");
...

Switching scenes will destroy this object as well as long as “DontDestroyOnLoad” isn’t applied to it. Just remove the destroy call.

Tip 2: Also if you are making this a multiplayer game doing something like this:

healthbar = FindObjectOfType<Healthbar>();

could potentially yield sketchy results if you plan to have more than one version of it in the scene at a time. From your script that seems to be indeed the case here. I would just explicity talk about the one your looking for

Example:

healthbar = this.GetComponent<HealthBar>();

So it could also be that if you have more than one of these in the scene it might be referencing the wrong one of it. Hope this helps.

Hi importguru88,

I downloaded your project. I made a couple of obvious fixes

  1. Currently everytime you run over a MedicalBox when your health is below 1000 it loads scene “T”. I changed it to apply the amount of “Health points gained” as set in the inspector instead of loading a new scene.
  2. Currently the “Sparkle Rising” area does nothing, I made a change/fix to apply the heal up to the player based on the “Heel up speed”.

Now for some questions, and forgive me since I do not know where you are going with this:

First question, why are you destroying the gameObject in ApplyDamage.OnTriggerEnter() when you are loading a scene on the next line.

Second question, this is just me but by the current logic in ApplyDamage.OnTriggerEnter()

if (healthbar.health <= 0)

why are you loading a different scene. Why not the same scene.

Third question, and this applies to my second question above and your question

"The problem I am having is the when the player health gets low the scene does not change .

If lets say the player runs into the Fire or the Flame and the health falls to 0, what do you want to happen? Currently the health is restored, after a brief delay, and then the player starts afresh in the current level."