- Home /
hitpoints problem
i have 2 scripts one that applys damage on contact to the tagged object and the other is the health points and for some reason they are nto working i am using unity 3.5 beta. var damage : float;
function OnCollisionEnter (other : Collision){
if (other.transform.tag == "Enemy"){
// Tell the object you hit that it was damaged
other.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
and
var hitPoints = 100.0; // this is the health; when <= 0 the object dies
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0) return;
hitPoints -= damage; // subtract damage from health
if (hitPoints <= 0) { // if health <= 0 the object goes to the grave:
// Destroy ourselves
Destroy(gameObject);
}
}
Where have you added the first script ? I think that this link might be of help, it seems to be a similar question:
http://answers.unity3d.com/questions/204197/damagescript.html
it did not help because when i added the script it didn't know what hitpoint was so i changed it to damage and its the exact same script i have made, after i took out all of the errors.
Answer by FLASHDENMARK · Jan 14, 2012 at 07:36 PM
function OnCollisionEnter (other : Collision){
if(other.gameObject.tag == "Enemy"){
// Tell the object you hit that it was damaged
other.gameObject.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
and
var hitPoints = 100.0; // this is the health; when <= 0 the object dies
function ApplyDamage (damage : float) {
hitPoints -= damage; // subtract damage from health
if (hitPoints <= 0) { // if health <= 0 the object goes to the grave:
// Destroy ourselves
Destroy(gameObject);
}
}
I think your problem was: if (hitPoints return; if health is less or equal to 0 you are stopping the rest of your code of executing because you are returning and therefore you are not destroying your object.
Another problem. I think SendMessage is type of GameObject not transform. So I changed it to other.gameObject.SendMessage, not sure though, but better safe than sorry ;)
I think Orange lightning is correct. Since subtracting HP isn't going to cause a crash or anything, you should just delete that check at the start altogether as he has done here.
i tried your scripts but it doesn't work i put the first one on my bullet i launch at it, and the second script on the alien. Am i doing this wrong? because i tried it with my own health script and it did not work here is what i used
var hitpoints : float;
function Awake (){ hitpoints = 100;
}
@conflictbliz what type of controller is your alien? Is it a rigidbody, CharacterController or neither?
it is just a model with a box collider i added a rigidbody and it still did not work, why does it need some sort of AI to work?
change it abit and i get this error
$$anonymous$$issing$$anonymous$$ethodException: The best match for method ApplyDamage has some invalid parameter. System.$$anonymous$$onoType.Invoke$$anonymous$$ember (System.String name, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object target, System.Object[] args, System.Reflection.Parameter$$anonymous$$odifier[] modifiers, System.Globalization.CultureInfo culture, System.String[] namedParameters) UnityEngine.SetupCoroutine.Invoke$$anonymous$$ember (System.Object behaviour, System.String name, System.Object variable) (at C:/BuildAgent/work/842f9557127e852/Runtime/Export/Coroutines.cs:18) UnityEngine.GameObject:Send$$anonymous$$essage(String, Object, Send$$anonymous$$essageOptions) Hitpoints:OnCollisionEnter(Collision) (at Assets/Hitpoints.js:4)
Your answer
![](https://koobas.hobune.stream/wayback/20220613062953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED]cant load lvl and i cannot figure out why. 1 Answer
Health C# script problem. 3 Answers
Expecting ) found = 1 Answer
health problem 1 Answer
How do i set health to regenerate over a specified range of time? 1 Answer