var curHealth = 100;
var maxHealth = 100;
var healthtext : GUIText;
var HitDamage : boolean;
var deathbomb : GameObject;
function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.gameObject.name == "Enemy");
{
HitDamage == true;
curHealth -= 10;
}
function Update(){
healthtext.text = "Health " + curHealth + " / " + maxHealth;
if(curHealth <=0){
Destroy(gameObject);
}
}
}
There are many many syntax errors in your script. Read your script like the computer would
line 10 :
if(hit.gameObject.name == "Enemy");
your semicolon closes the if statement, so nothing happens after that, and lines 11-15 would be considered an error. No semicolon after the conditional of an if statement.
line 12 :
HitDamage == true;
== is used to check if something is equal to, to make something equal something else, just use one equals sign.
line 17 :
healthtext.text = "Health " + curHealth + " / " + maxHealth;
UI don’t use Unity GUI so am not sure if you can pass an int to a string this way, I assume not. TO convert something to a string, use .ToString()
Curly Braces :
Your ControllerCollider function doesn’t have the closing curly braces until after your Update function. Make sure each function is enclosed between {}
Declaring variables :
var curHealth = 100;
this is a personal thing as unity can fix this for you when compiling, but you should really typecast all your variables. That also proves you know what a variable is and how it can be interacted with. use
var curHealth : float = 100; or var curHealth : int = 100;
So I have editedyour script with these considerations. Please take the time to read it and yours, discover the differences and learn from it.
var curHealth : int = 100;
var maxHealth : int = 100;
var healthtext : GUIText;
var HitDamage : boolean;
var deathbomb : GameObject;
function OnControllerColliderHit( hit: ControllerColliderHit )
{
Debug.Log( "Controller hit : " + hit.gameObject.name ); // what did the controller collide with?
if ( hit.gameObject.name == "Enemy" ) // if is equal to
{
HitDamage = true; // equals
curHealth -= 10;
}
}
function Update()
{
healthtext.text = "Health " + curHealth.ToString() + " / " + maxHealth.ToString(); // ToString()
if ( curHealth <= 0 )
{
Destroy( gameObject );
}
}