I am playing animation. What is going on is the script is trying to switch scene when health reach zero . I need to scene to switch like twenty seconds later so my animation play way through. The scene is switch on the ModifyHealth function Health. Here is my script :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[System.Serializable]
[RequireComponent(typeof(SpriteDatabase))]
public class Healthbar : MonoBehaviour {
/// <summary>
/// Play a death animation or enable the ragdoll when the character dies.
/// </summary>
public int fontSize;
public static int playersHealth;
public int health;
int healthNormalized;
GameObject player;
Image frame;
Image bar;
public int displayCritical;
public int displayRedBar;
public int displayYellowBar;
public string healthMessage;
public string criticalMessage = "Critical";
public string playerTag;
Text Message;
Text Critical;
public bool showHealthValue;
public bool showCritical;
public string sceneToLoad = "T";
SpriteDatabase sd;
public Theme chosenTheme;
public FontNames chosenFont;
int myTheme;
int myFontTheme;
public enum Positioning {
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
[HideInInspector]
public bool alive = true;
//For demo purposes, store player's initial transform (so later it can be respawned there)
Vector3 startPos;
//used to choose between left or right alignment
public Positioning positioning;
//On Start, assign SpriteDatabse class to 'sd'. (Note: That class can never be missing due to the dependency system)
//It then runs Debugger() (find it below.) It checks whether the required sprites are assigned in the inspector, etc.
//Then, it builds hierarchy for GUI (find below)
void Start(){
sd = GetComponent<SpriteDatabase>();
fontSize = Mathf.Clamp(fontSize, 5, 30);
Debugger();
BuildHierarchy();
startPos = player.transform.position;
}
//Converts health integer to float value and updates it every frame.
//Keeps the GUI bar (image) fill amount value synchronized with the health value.
//Note: healthNormalized cuts the number so that it's on a 100 scale like in every game (it's basically the percentage)
void FixedUpdate(){
if (player) {
if (alive) {
/*
if (healthNormalized <= 0) {
alive = false;
die();
}
*/
healthNormalized = health/10;
//Converts health value to a float (range 0-1) so it can be used for image.fillamount
float healthValue = health * 0.001f;
healthValue = Mathf.Clamp(healthValue, 0, 1);
//Checks if it's time to turn the bar color to red or yellow (replace the sprite basically)
CheckForBarColor();
bar.fillAmount = healthValue;
}
DisplayText();
}
else
player = GameObject.FindGameObjectWithTag("Player");
}
void DisplayText(){
if (showHealthValue)
Message.text = healthMessage + ": " + healthNormalized.ToString();
if (healthNormalized <= displayCritical && alive && showCritical) {
Critical.enabled = true;
}
else
Critical.enabled = false;
}
//Called by every object affecting player's health.
//Class that calls it: ApplyDamage
//See that for more info on how to use it!
public void ModifyHealth(int amount) {
if (alive)
health = health - amount;
if (health <= 0) {
Debug.Log("1: sceneToLoad = " + sceneToLoad);
if ((sceneToLoad != "") && (SceneManager.GetSceneByName(sceneToLoad) != null)) {
Debug.Log("2: sceneToLoad = " + sceneToLoad);
Invoke("loadNewScene",25);
}
}
else {
health = Mathf.Clamp(health, 0, 1000);
}
}
void loadNewScene()
{
SceneManager.LoadScene(sceneToLoad);
}
//Modify this to change the way of dieing (this just for the demo scene (respawn player at starting location after 2 seconds)
//Find IENumerator at the very bottom of the code.
void die(){
StartCoroutine(Resurrection());
}
//Changes bar color depending on what values are set in the inspector
void CheckForBarColor(){
if (healthNormalized > displayYellowBar)
bar.sprite = sd.sprites[myTheme].GreenBar;
else if (healthNormalized > displayRedBar)
bar.sprite = sd.sprites[myTheme].YellowBar;
else
bar.sprite = sd.sprites[myTheme].RedBar;
}