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 darkironphoenix · Jul 01, 2012 at 11:17 AM · raycasttag

Applying damage to an object

My character needs to interact with items. In this case, he needs to be able to cut down trees. Most of the code for it i have worked out, the bit i am stuck on is how to apply damage to an object when clicked on.

So far i have this:

 function Update () {
 
 var hit : RaycastHit;
 
 if (Input.GetButtonDown("Fire1")) {
     if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
         Debug.Log(hit.collider.tag);
             if(hit.collider.tag == "Tree") {
                 Debug.Log ("tree");
                 SendMessage ("ApplyDamage", 5.0);
             }
         }
     }
 }

with this attached to the tree object

 function ApplyDamage (damage : float) {
     print (damage);
     }

However, although it can detect that the item is a tree on a click, it throws up the error:

SendMessage ApplyDamage has no receiver! UnityEngine.Component:SendMessage(String, Object) mousefire:Update() (at Assets/Scripts/Gameplay/mousefire.js:16)

I will be missing something here, still finding my way around code :) Any help will be welcome.

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 whydoidoit · Jul 01, 2012 at 11:19 AM

Try

  hit.collider.gameObject.SendMessage("ApplyDamage", 5);

You are sending the message to the calling object at the moment.

Comment
Add comment · Show 4 · 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 darkironphoenix · Jul 01, 2012 at 11:32 AM 0
Share

i get this error:

Assets/Scripts/Gameplay/mousefire.js(16,37): BCE0019: 'gameObject' is not a member of 'UnityEngine.RaycastHit'.

avatar image whydoidoit · Jul 01, 2012 at 11:33 AM 0
Share

Sorry, updated the answer above.

avatar image darkironphoenix · Jul 01, 2012 at 11:44 AM 0
Share

Perfect, thanks for the speedy reply. You must just be in it for the hugs from random people. :)

avatar image whydoidoit · Jul 01, 2012 at 11:45 AM 0
Share

$$anonymous$$aybe that's it!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

casting a raycast from the raycast hit.point 1 Answer

find tag from raycast 1 Answer

Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer

Raycast Based on GameObject Tag 0 Answers

why doesn't this Raycast compare tag work? C# 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