Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LexGear · Nov 27, 2012 at 01:21 PM · gameobjectraycastfunctionweaponaccess

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LexGear · Nov 27, 2012 at 02:57 PM 0
Share

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.

avatar image Shrandis · Nov 27, 2012 at 03:32 PM 0
Share

Yes you can use hit.collider.Send$$anonymous$$essage for that purpose and only the object you Send$$anonymous$$essage will receive it.

avatar image LexGear · Nov 28, 2012 at 08:11 AM 0
Share

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?

avatar image Shrandis · Nov 28, 2012 at 08:21 AM 0
Share

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

avatar image DeveshPandey · Nov 28, 2012 at 10:13 AM 0
Share

are you want to call applyDamage() when gameobject collide with other?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges