- Home /
Accessing Functions in other gameObjects
Hi Guys, this is driving me CRAZY! I've been at this for over a week now and can't find an answer.
I've got a player, and a bunch of zombies.
The zombies are dropped in via prefab.
The player has a weapon, when fired, it shoots a ray.
Zombies spawn and self create themselves.
Ok, my problem is, the zombies have a 'applyDamage()' function on themselves as well as a life variable (private var life = 50). How do I trigger that function to damage that zombie only (one that was hit by ray). So far I've only managed to damage all zombies.
Answer by Shrandis · Nov 27, 2012 at 01:55 PM
After raycasting, you simply check if hit was a "zombie" (in the example, I did this by checking tag) If it was a zombie, you then call GetComponent to access the zombie script, then call their ApplyDamage method. (replace "Zombie" betwen < and > with your zombie script's name, whatever it is)
Here is a c# example (I don't use UnityScript so I can't post a UnityScript example):
Ray ray = Camera.main.ScreenPointToRay(crosshairPosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Zombie")
{
hit.collider.GetComponent<Zombie>().ApplyDamage(playerDamage);
}
An alternative is replacing the GetComponent call with:
hit.collider.SendMessage("ApplyDamage", playerDamage);
Remember that if you are using c#, you need to make ApplyDamage method "public" or you won't be able to access it from another script.
Please don't forget to click the checkmark near the answer if it solves your problem or post comments if something confused you.
Alright, so I'm coding in Javascript, but I think I get whats going on here. Can I use hit.collider.Send$$anonymous$$essage to activate a function on an object. And if that object shares a script with another objects, the function will only take effect on the hit object? Ok, I'll give this a go, and let you know what happens.
Yes you can use hit.collider.Send$$anonymous$$essage for that purpose and only the object you Send$$anonymous$$essage will receive it.
Ok, I've added in hit.collider.Send$$anonymous$$essage("ApplyDamage", weaponDamage); but i'm getting the error "ApplyDamage has no receiver". Is there something I'm meant to do on the zombies end?
That error means that the object you're sending message to does not have any script with a function named "ApplyDamage"
You have to make sure the script attached to zombie contains a function exactly named ApplyDamage which accepts that weaponDamage as parameter.
Also, make sure you actually check if hit.collider.gameobject is a zombie. You can do that by assigning a "Tag" to your zombie prefab and then checking if hit.collider.tag is equal to the tag you assigned.
$$anonymous$$ore info about tags if you need: http://docs.unity3d.com/Documentation/Components/Tags.html
are you want to call applyDamage() when gameobject collide with other?
Your answer
Follow this Question
Related Questions
How to call and run a function from another script on a transform. 0 Answers
Select Object ( Weapon ) Quick! Help! xD 1 Answer
See what object I hit in raycast 0 Answers
How do you check what function your animation is calling? 0 Answers
Camera switch between child back to main camera issue 2 Answers