- Home /
How to make my Player take damage from prefab?
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);
}
}
}
Answer by AlucardJay · Feb 25, 2013 at 05:51 AM
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 );
}
}
Hello. If this answer helped, could you please mark it as accepted. Thanks.
On the left-hand-side of the Answer box , there are the following icons :
Thumb Up
Number (of votes)
Thumb Down
A Tick/Check $$anonymous$$ark
If an answer worked for you, click on the 'Tick/Check mark', the answer should now be highlighted in green and marked as accepted.
http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Your answer
![](https://koobas.hobune.stream/wayback/20220613093230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player Respawn After Death 1 Answer
Problems with a player spawning and handling script 1 Answer
Enemy Damage 1 Answer