Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by MRDJR97 · Oct 26, 2017 at 04:24 PM · sendmessagefunction callsending objecthealth-deductionhealth

SendMessage function not working

Could anyone help me with this it has me stumped for days..

I am trying to deduct health from a second game object by using the sendmessage function.

MeleeSystem.js

 #pragma strict
 
 var TheDamage : int = 50;
 var Distance : float;
 var MaxDistance : float = 20;
  
 function Update()
 {
         if (Input.GetButtonDown("Fire1"))
         {
         
    
                 var hit : RaycastHit;
                 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
                 {
                     Distance = hit.distance;
                     if (Distance < MaxDistance) 
                     {
                         Debug.Log("11111");
                         hit.transform.gameObject.SendMessage("ddd", 50, SendMessageOptions.DontRequireReceiver);
                            hit.transform.gameObject.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
                       }
             }
         }
 }

EnemyLogic.js

 #pragma strict
 var Health=100;
 
 function Update(){
     if(Health<=0){
         Dead();
     }
 }
 
 function ApplyDamage(theDamage: int){
     //Health-=theDamage;
 
     Debug.Log("22222");
     Health-=50;
 }
 
 function Dead(){
     Destroy(gameObject);
 }
 function ddd(ge : int){
     Debug.Log("3333");
 }



2222 and 33333 never print out so its definitely the SendMessage Function that's not working...

Comment
Add comment · Show 2
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 Hatsuko · Oct 26, 2017 at 08:38 PM 0
Share

Print out your hit.transform.gameObject to check if it's actually the object with EnemyLogic.js that gets the message. I remember you can use something like (syntax not sure) Debug.Log(hit.transform.gameObject, hit.transform.gameObject.transform) so that when you click on Console message it'll bring you directly to the object.

avatar image MRDJR97 · Nov 08, 2017 at 05:41 PM 0
Share

I did, it is referencing the "ground" object only. Doesn't seem to recognise my enemy object.

1 Reply

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

Answer by MRDJR97 · Nov 08, 2017 at 05:47 PM

I've figured it out, the raycast was firing at a weird angle (about 45 degrees) on the x axis, so it wasn't hitting my enemy object. Don't know why.

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.

Follow this Question

Answers Answers and Comments

119 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Health system in a text based game. 2 Answers

Why is this script not working? 2 Answers

how to put in a delay in void 1 Answer

Unity crashes ONLY when this function gets called many times per second. Help would be very appreciated. 0 Answers

Referencing playerHealth (from a separate script) in an if statement 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