• 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 Wh1teSw1p3 · Sep 02, 2015 at 02:08 PM · raycastdamagehitsendmessage

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 ?

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

2 Replies

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

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.

Comment
Add comment · Show 2 · 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 Wh1teSw1p3 · Sep 02, 2015 at 03:08 PM 0
Share

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);

avatar image Wh1teSw1p3 · Sep 02, 2015 at 03:49 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Findout if an object that was hit by raycast is still being hit (30 object that are instantiated) 0 Answers

RayCast to send message to the object it hits? 1 Answer

How do I check if raycast is hitting a gameobject? 1 Answer

simple way to send a message threw a raycast 1 Answer

How can I get back a hit? 1 Answer


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