- Home /
Raycast with SendMessage can send Parameter, but how to send a variable ?
Hello Guys, I am relatively new to Unity but normally i can figure out any problem. But this time, i dont find anything about it or at least i cant find a working answer, so if it exists already, just post the link. My Problem is that, i have a Raycast, it hits my Target and with 'SendMessage' i apply damage to it: hit.transform.SendMessage ("ApplyDamage", 1.0F, SendMessageOptions.RequireReceiver); - (function on the target)- void ApplyDamage (float damage) { healthPoints -= damage; } and the target looses 1 health, so this works just fine, but instead of 1.0f I want to send a variable ('Damage'), so that the amount of health lost depends on that variable, but just replacing the 1.0f with the variable wont work, so how could I do this ?
Answer by Landern · Sep 02, 2015 at 02:12 PM
There shouldn't be any issues whatsoever sending a float variable in place of an explicit value.
float damage = 1.0f;
hit.transform.SendMessage ("ApplyDamage", damage, SendMessageOptions.RequireReceiver);
should be just fine as well as doing some work on the value of damage
float damage = 1.0f * 10 * time.deltaTime; // or whatever
hit.transform.SendMessage ("ApplyDamage", damage, SendMessageOptions.RequireReceiver);
SendMessage for the value passed takes an object, the very base of all things(for the most part) and matches your methods/functions by the signature. In case above, it will look for a method/function called ApplyDamange with a parameter/argument of type float as you have already noticed i'm sure, this should be no different when passing an explicit variable.
I thought so, but as soon as i replace the 1.0f (of course it works with any other number) with damage (or however i call my float variable), it just wont work anymore, that's what confuses me so much about it. That works: hit.transform.Send$$anonymous$$essage ("ApplyDamage", 1.0f, Send$$anonymous$$essageOptions.RequireReceiver); This doesn't: hit.transform.Send$$anonymous$$essage ("ApplyDamage", damage, Send$$anonymous$$essageOptions.RequireReceiver);
After all this should work (what it does as long as my Parameter is a float and not a float-variable ^^) i think i'll reinstall unity and test it out again, see if it works then, because everyone else says it should work, too.
Answer by Wh1teSw1p3 · Sep 03, 2015 at 01:39 AM
Ok, it was definitly a bug, just for 'fun' i made a new float (exactly the same as before, just different name, damage2) and replace the parameter name with that, and it works now. Whatever this was, using the variable damage doesn't work and damage2 DOES WORK for some reason.
Still, thank you @Landern