- Home /
Overloaded functions fail to call
I am testing new damage functions which will allow headshots and explosion knockback etc, by using multiple versions of the damage function with different parameters, so not all damaging items have to find all the parameters if it's not needed, and so they can use SendMessage instead of directly calling a function.
I am testing it with this code:
function Damage () {
print("damage, no parameters");
}
function Damage (damage : float) {
print("damage, amount given: "+damage+" no other info");
}
function Damage (damage : float,type : DamageType){
var typeName="Unknown";
if(type==DamageType.RayBullet)
typeName="Raycast Weapon";
if(type==DamageType.RealBullet)
typeName="Bullet Weapon";
if(type==DamageType.Melee)
typeName="Melee";
if(type==DamageType.Explosive)
typeName="Explosive";
if(type==DamageType.Collide)
typeName="Collision";
if(type==DamageType.Splatter)
typeName="Splatter";
if(type==DamageType.Guardians)
typeName="Guardians";
print("damage, amount and type :"+damage+", "+typeName);
}
function Damage (damage : float,type : DamageType,hitCollider : Collider){
var typeName="Unknown";
if(type==DamageType.RayBullet)
typeName="Raycast Weapon";
if(type==DamageType.RealBullet)
typeName="Bullet Weapon";
if(type==DamageType.Melee)
typeName="Melee";
if(type==DamageType.Explosive)
typeName="Explosive";
if(type==DamageType.Collide)
typeName="Collision";
if(type==DamageType.Splatter)
typeName="Splatter";
if(type==DamageType.Guardians)
typeName="Guardians";
print("you just recieved "+typeName+" damage on "+hitCollider.ToString()+". You took this much damage: "+damage);
}
but when using a SendMessage that sends a float damage amount, the no paramteter version is called, and it returns this error: "Failed to call function Damage of class PlayerRigidController Calling function Damage with 1 parameter but the function requires 3." The two parameter version seems to work though, so I don't know what is going on.
Please could someone help me fix this?
How are you calling the 1 parameter version? $$anonymous$$aybe you're using an int and UnityScript doesn't (know how to) convert that.
It's a definite float, as it uses AnimationCurve.Evaluate multiplied by a float to work out the damage at a certain distance from the player
Answer by ArkaneX · Aug 10, 2013 at 10:24 AM
It seems SendMessage does not support overloads. There are some other questions, e.g. here related to this issue.
As a solution, you could create a method with one parameter, a class or struct with three properties - Damage, Type, HitCollider.
I changed the muliple parameter versions names to Damage1 to seperate them from the Damage function (now only the (damage : float) version), but although it works as it should, it still gives the exact same error. It's an improvement though because before it was calling the complete wrong function.
Do I understand correctly, that after changing overload with 3 parameters to new method Damage1, and calling method Damage you still get "[...] with 1 parameter but the function requires 3" error? If yes, then I believe you're somehow still using the previously compiled dll.
So it's a problem with the software taing time to catch up, not with my code? I will try restarting unity, I think I have had issues like that before.
Still happens, but it's working anyway, so I'm not going to worry about that error any more unless it starts breaking things.
I don't understand how it can work if the error still occurs... Anyway - if you need more investigation, leave a comment :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613111917im_/https://answers.unity.com/themes/thub/images/avi.jpg)