- Home /
Character controller mess up damage
ok well i got a damage and health script to run between two cubes hitting each other, this was a test for a simple player health script. When i added this script to anything with a character controller it suddenly stopped working and in fact, was even detecting that there was a collision between the two objects. I was hoping someone on here could help me find a solution to this problem as it's been bugging me for a while now.
heres the health script
var healthMax = 100; var curHealth = 100; var dieHealth = 0.0;
function ApplyDamage (damage : float) {
curHealth -= damage;
print (curHealth);
if(curHealth <= dieHealth) {
die();
}
}
function die () {
Destroy(gameObject);
}
and heres the damage script
var damage = 10;
function OnCollisionEnter (col : Collision) {
col.gameObject.SendMessageUpwards("ApplyDamage", damage); print (damage); Debug.Log ("Hello");
}
Answer by AngryOldMan · Apr 11, 2011 at 12:49 PM
Why don't you put both scripts together? Then you don't have need for the send message function. Also maybe try explicit coding (i believe that's the term if not please someone correct me) in which you state what each variable is, ie. float, int, boolean etc. Here I've cleaned up your code, complied it into one script and added some suitable debug logs, try using that and seeing what errors and logs you get.
var healthMax : int = 100; var curHealth : int = 100; var dieHealth : int = 0.0; var damage : int = 10;
function ApplyDamage (damage : float) { Debug.Log ("Apply Damage"); curHealth -= damage; print (curHealth); if(curHealth <= dieHealth) { Debug.Log ("called die function"); die(); } }
function die () { Destroy(gameObject); Debug.Log ("died"); }
function OnCollisionEnter (collision : Collision) { ApplyDamage(); Debug.Log ("Collision entered"); }
Answer by Kyle 11 · Apr 12, 2011 at 11:33 AM
Assets/All Scripts/Player Health.js(27,15): BCE0017: The best overload for the method 'Player Health.ApplyDamage(float)' is not compatible with the argument list '()'.
this is the error that the scripts giving me.
Answer by Ves · Apr 29, 2011 at 06:41 PM
Basically it's saying that the function ApplyDamage that you've defined requires an argument (i.e. a float value which represents damage). Your OnCollisionEnter function calls ApplyDamage, but does not pass it an argument, which is required.
The last function in the above script should be:
function OnCollisionEnter (collision : Collision)
{
ApplyDamage(damage);
Debug.Log ("Collision entered");
}