- Home /
Life Player loads another level
Hello everyone.
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 am sorry but I don't understand it. What is your question? You want help with editing the existing script to your desire?
Hello Hexer. Currently the two scripts are not working.They have already changed. In practice when I start the game to test the first death of player number 3 appears on the screen it is 3, then the player disappears. You think that the script are correct? I have error? thank you
These scripts are faulty, for what you want to accomplish. Life will actual never hit zero. Because in the Update it will change the int "life" with +3 and -1. resulting in +2 each update.
in this script that name should I give? where do I add it?
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;
}
You can name the script whatever you want. Where you put this script depends on the method you want to use to reduce the lives of the player. The important part of the script is (Health.life -=1;) which reduces the life with 1 . You can put that line of code in an OnTriggerEnter(Collider other) function and attach that to the player.
For example if you have a player and an obstacle tagged as "spikes" you just do.
void OnTriggerEnter(Collider other){
if(other.tag == spikes){
Health.life -= 1;
}
}
for each time the player walks into a collider with the tag "spikes" it will reduce the life with 1 Be sure to tag the obstacle with "spikes" and put a collider on it and have the isTrigger ticked to true. Then check if the player has also a collider and attach a rigidbody to the player.
Answer by Hexer · Jul 24, 2015 at 10:36 PM
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. https://unity3d.com/learn/tutorials/topics/scripting
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.
Thanks again Hexer. Try your script tomorrow, then let you know everything works itself. :-)
Hello hexer. I finally solved !! I was all day editing scripts. The problem was. In the script Contovita. Thank you for your help. :-)