I have a script that detects if a particle hits the player attached to the player, and a script the main camera that manages health and other statistics. I was wondering how to tell the script on the main camera to subtract health from the other script.
I’m still trying to work this out my self but another method could be to use Message.sendMessage or something like that, check out the documentation
I got lost reading the question concerning the specifics. Particularly at “hits the player attached to the player”.
if (hit.collider.tag == "enemy"){
hitObject = GameObject.Find(hit.rigidbody.name);
var dist = Vector3.Distance(hitObject.transform.position, transform.position);
theDamage = Random.Range(0, 50)+ 100-(dist*10);
hitObject.SendMessage ("ApplyDamage", theDamage);
}
then in the script where I tracked damage:
function ApplyDamage(damage : float){
myHealth -= damage;
}
there might be a better way to get the “hitObject” in my case, but it works.
Thank you for reading for me ![]()
I know I am lazy but that’s what forums are for , getting other people to read for you
you can just use:
hit.collider.gameObject.SendMessage("ApplyDamage", theDamage);
no need to create a hitObject variable or use GameObject.Find
What about if there are multiple instances of a function
How does having overloaded functions interfere with anything?
Just don’t make multiple versions of functions that you’re going to call with SendMessage.
I meant if there are two classes both with the same function within them are they both called?
Only if they’re both attached to the gameobject you’re sending the message to.
That’s partly the appeal of having a sendmessage function - you don’t have to know what script you’re sending it to, just that everything on the gameobject will know when the message happens.
Thanks for clearing that up now I’m looking forward to easier coding -sigh-