- Home /
How to increase the life of the player?
Hi all! I plugged in my palyer life. every time I hit me a lever life. I want my player is now being increased life by taking a cube and doing so increases my life.
I am using this script to the player.
health control:
var health1 : Texture2D;
var health2 : Texture2D;
var health3 : Texture2D;
var health4 : Texture2D;
static var LIVES = 5;
function Update ()
{
switch(LIVES)
{
    case 4:
         guiTexture.texture = health4;
    break;
case 3: guiTexture.texture = health3;
break;
case 2: guiTexture.texture = health2;
break;
case 1: guiTexture.texture = health1;
break;
case 0:
Application.LoadLevel(0);
    break;
    
    
}
}
use the "code" button (0101010) to format code. click EDIT and you get to make it tidy !!
Are you asking about collision or how to edit the '-' character into a '+' character? =/
(And I'm not even going to say anything about how an array would optimize your code by a lot.)
Answer by andytouch · Sep 28, 2012 at 01:55 PM
Firstly, use an Array to tidy up your lives GUI. It will keep your code clean and much more manageable and efficient.
Secondly, you would want to setup a collision on the player that when they collided with the cube, it will run this:
 lives +=1;
Answer by Cyber-X-Zone · Sep 28, 2012 at 01:50 PM
Add such a script to the trigger or the cube which increases a life that the script in the cube connects to the life script in player and increase it.... For Example:: The script for the cube can be like this:
 var lifeAmount = 1;//life to be added to the player
     //Change the playerLife to the name of script attached to the //player
     function ApplyPickup (playerLifeScript : playerLife)
 {
 
 switch (lifeAmount)
 {
 case 0:
    playerLifeScript.AddHealth(lifeAmount);
    break;
 
 function OnTriggerEnter(col : collider) {
 var playerLifeScript : playerLife = col.GetComponent(playerLife);
 }
And for the player, the script can be named as playerLife... var health : int = 6; var maxHealth : int = 6;
 function AddHealth (powerUp : int)
 {
     health += powerUp;
     
     if (health>maxHealth)// We can only show six segments //in our HUD.
     {
         health=maxHealth;    
     }        
 }
Your answer
 
 
             Follow this Question
Related Questions
3 Lives GUI 1 Answer
Life Bar controlled 1 Answer
Regain health on GUI 2 Answers
Life Player loads another level 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                