- Home /
Health script and damage script dont work.
these scripts dont work for some reason, i wrote them mostly myself using references, but appearantly i did something i shouldnt have when writing them, my players doesnt get health as far as i know, and my damage script doesnt deal 10 points of damage to my player on collision. how could i fix it so that it does work?
//player Health script.js//
public var playerCurHp;
public var playerMaxHp;
playerCurHp = 100;
playerMaxHp = 100;
function ApplyDamage (damage : int) {
playerCurHp -= damage;
Debug.Log(playerCurHp);
if(playerCurHp > playerMaxHp){
playerCurHp = playerMaxHp;
}
if(playerCurHp < 0){
playerCurHp = 0;
}
if(playerCurHp < 1){
//die
// Die();
Debug.Log("YOU DIED!");
}
//function Die () {
//Application.LoadLevel (Application.loadedLevel);
}
//**apply damage script.js**//
var damage = 10;
function OnCollisionEnter (col : Collision) {
col.gameObject.BroadCastMessage("ApplyDamage", damage,
SendMessageOptions.DontRequireReceiver);
}
"my players doesnt get health as far as i know" is the fundamental problem here. Start the debugger and check code execution and runtime variable states. Does DoDamage actually get called? Do the variables change the way you expect them to change? If everything behaves the way you expect it to at runtime you can ask specific questions.
are you saying to try making collision to force a debug.log of players health? if so, yes i tried that and nothing was ever debugged.
No, I'm saying you should learn and understand the $$anonymous$$onoDevelop debugger: http://unity3d.com/support/documentation/$$anonymous$$anual/Debugger.html
Alternatively, using Debug.Log statements to print your variable states would be a good start.
oh, i see, thanks, and i tried the alternative. as shown in the code, but it never debug.logged anything.
That means that your BroadCast$$anonymous$$essage isn't being received. Put a Log in OnCollisionEnter to see if that function is being called. With the debugger, you can set breakpoints to do the same checks easier.
Answer by Punkjim420 · Jun 22, 2012 at 03:17 AM
oh, i see, i looked at this, couldnt quite figure out how to do this method of checking. the debugger freezes and lags on my computer when i try to run it, but i did fix my problem by making it a collision check in the health script and if the collided object is tagged as harmful, i lose health by a set amount. ^^ thanks for your help.
Answer by Berenger · Jun 21, 2012 at 09:14 PM
To what is attached apply damage script.js ? Because it's going to call ApplyDamage on whatever is colliding with what this script is attached to. Meaning, if this is attahe to the player, ApplyDamage will be called on the bullet or whatever does the damage, and the don't have that function. Either send the message on the same gameObject, or use GetComponent to find the health script.
By the way, you're making sure that currHealth is between min and max, there is a function for that : Mathf.Clamp.
ok thanks i did that now. inside the same script i call a collider check using tags to find what the object touches and if its the set object it removes the set value of health wanted. :) solved. thanks for your help.
Glad you solved your problem. You should mark this question as solved now by pressing the green check mark on the answer.