I’m editing a script to allow the character to die three times, and then load the level gameover.
I am using this script HealthBar, to allow after 3 times the player dies, the charge level should gameover.
Suggested some?
Thank You.
healthbar script:
using UnityEngine;
using System.Collections;
public class HealthBar : MonoBehaviour
{
public Player Player;
public static int life = 5;
public Transform ForegroundSprite;
public SpriteRenderer ForegroundRenderer;
public Color MaxHealthColor = new Color(255 / 255f, 63 / 255f, 63 / 255f);
public Color MinHealthColor = new Color(64 / 255f, 137 / 255f, 255 / 255f);
public void Start()
{
}
public void Update ()
{
life += 3;
var healthPercent = Player.Health / (float) Player.MaxHealth;
ForegroundSprite.localScale = new Vector3(healthPercent, 1, 1);
ForegroundRenderer.color = Color.Lerp(MaxHealthColor, MinHealthColor, healthPercent);
life -= 1;
if(life <= 0)
{
Application.LoadLevel("Resuscita Argon");
life --;
GameObject.Find("Vita3").guiText.text = ""+life;
print("Prova Vita" + life + " Cazzo non funziona!!!");
}
}
}
script count life:
using UnityEngine;
using System.Collections;
public class Contovita : MonoBehaviour {
public static int life = 5;
// Update is called once per frame
void Awake () {
guiText.text = ""+3;
print(life);
}
}
I guess you picked a script from Internet and want to edit it some bit.
If you did then you will see that you don’t need some codelines.
I wrote a sample script for you.
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour
//Total Lifes you start with.
public static int life = 3;
void Update(){
//If you have zero lives left it will go to the Level " GameOver"
//The Scene called "GameOver" should be added to the build and run scene.
if(life <= 0){
print("GameOver, You have no lives left")
Application.LoadLevel("GameOver");
}
}
in another script. // If a condition is met, invoke " LifeReduction" (This will reduce the lives you have left with -1)
//If you want to invoke this function within another script, then it is needed to add static public before void.
//Invoke this functiom can be done by doing someting like X.LifeReduction();
//X is the variable for how you named the script where you put this code.
static public void LifeReduction(){
//Remember that you have to invoke "LifeReduction" within another function.
//How you do this depends on the condition, by colliding? raycast? or a loop?
//Reduces Life with -1.
Health.life -= 1;
}
If you want to display the total amount of lives on the screen, then you can do that by using string method to GUIText. Or using a sprite as indicator for the total amount of lives you have.
If you don’t know how to script or don’t understand script that much. I highly recommend you to get some practice and some basic knowledge about how to script inside of MonoDevelop. I also began from scratch without help from professional tutors, learning from tutorials I found on the Unity site and Youtube.
Unity has an 28 episodes long tutorial, introducing you to basics of C# in Unity.
After these 28 episodes I guarantee you, you will be able to make your own script with help of using the Unity Reference. And you will understand scripts a lot better.