how do i create a max lives on this script because when i pick up lives
it still counts up
and how do i make that i cant pick up health-pickup when i am full-health?
could someone help me ?
this is my healthcontrol:
var health1 : Texture2D; //one live left
var health2 : Texture2D; //two live left
var health3 : Texture2D; //three live left
var health4 : Texture2D; //four live left
var health5 : Texture2D; //full health
static var LIVES = 5;
function Update ()
{
switch(LIVES)
{
case 5:
guiTexture.texture = health5;
break;
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:
//gameover script here
break;
}
}
and here the pickup piece:
private var heart = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag== "heart")
{
gotHit = true;
//Add Lives here !! YEAH !
HealthControl.LIVES += 1;
Destroy (hit.collider.gameObject);
}
}
------------------------------------------
here comes the other piece in same javascript:
function LateUpdate()
{
if(heart)
{
heart = false;
}
}
You have all the code here Well, aside from var maxLife = . Vicenti’s code just needs to be put in your pickup code … this is a pretty logical thing to do, and considering the first and second parts of the code you produced, it should be a valuable learning exercise to combine them both. Good Luck!
Look at the code you already have (you already have LIVES) and observe how variables are declared.
Declare a variable named something that makes sense to you. It will represent the maximum value of LIVES.
Because it is so closely tied to the variable LIVES, it makes good scripting sense to declare it near LIVES, and definitely in the same script as LIVES.
Before attempting to pick up an object, check to see that LIVES is less than your declared maximum value for LIVES.
You attempt to pick up an object in the OnControllerColliderHit function. That is where you perform the check.
I feel that you may wish to read more tutorials if this still doesn’t make sense to you.
i got a little question because i have placed my script in it but i cant pickup health now anymore…
this is the script for now…:
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if ( LIVES < maxLIVES ) {
// pick up object
if(hit.gameObject.tag== “heart”)
{
gotHit = true;
//Add Lives here !! YEAH !
HealthControl.LIVES += 1;
Destroy (hit.collider.gameObject);
}
} else
// don’t pick up object
{
if(heart)
{
heart = false;
}
}
}
and also the two variables:
static var LIVES = 5;
static var maxLIVES = 5;
could someone help me ?
(already thnks to vicenti)
Hey, you are so close to the answer - you have all the bits. Just telling you what to do wouldn’t help you in the future, this is a simple thing, and learning to do it yourself would not only make you feel awesome, it would make you a better coder. You have everything you need.
You already have one if statement in your code learning to combine if statements is pretty important Just try different things, I am sure you will manage it!
Are you sure the object you want to pick up has a “heart” tag? If you click on the tag menu in the inspector, you can add new tags, which you will need to do in this case.