Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 El-Deiablo · Jan 02, 2017 at 08:45 PM · raycastsendmessagedecreasehealth

Raycast SendMessage not working properly

I came across the SendMessage method and cannot get it to work. I want health to be deducted from the enemy when the raycast hits the enemy. Here's my code:

EnemyScript

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
 
     public int enemyHealth = 10;
 
 
     void DeductPoints(int damageAmount){
 
         Debug.Log ("Hey!");
         enemyHealth -= damageAmount;
 
     }
 
     void Update () {
 
         if (enemyHealth <= 0) {
 
             Destroy (gameObject);
 
         }
 
     }
         
 }

HandGunDamage Script

using UnityEngine; using System.Collections;

public class HandGunDamage : MonoBehaviour {

     public int damageAmount = 5;
     public float targetDistance;
     public float allowedRange = 15;
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetButtonDown("Fire1")){
 
             RaycastHit hit;
 
             if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit)) {
 
                 targetDistance = hit.distance;
 
                 if(targetDistance < allowedRange){
 
                     
                 hit.transform.SendMessage("DeductPoints", damageAmount, SendMessageOptions.DontRequireReceiver);
                     Debug.Log ("Hit");
 
                 }
 
             }
 
         }
     
     }
 
 }

Comment
Add comment · Show 1
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 juicyz · Jan 04, 2017 at 12:30 AM 0
Share

Is the "Hit" message being displayed? Also a many similar questions were asked. Here is an example. http://answers.unity3d.com/questions/1158543/sendmessage-not-calling-function-in-other-script.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 04, 2017 at 12:11 PM

not working properly

is not a proper description of your problem. What do you see in your console? How is your enemy structured? Does it have several colliders? Where's the target script located?

Since a lot information is missing we can only guess.

You most likely are "hitting" a child collider but your script is located on a parent. You can use SendMessageUpwards instead. It will do the same as SendMessage but if the message isn't picked up by the object you've hit it will go up the hierarchy and try it again on the parent. It will go all the way up to the top if no object has that method.

Such things are trivial debugging problems that you should be able to figure out yourself. Start by printing the name of the object you have hit. Debug.Log can also receive an additional "context" parameter. When you pass in the object you have hit, you can simply click the debug message in the console and Unity will "ping" / highlight the object in the hierarchy / project panel.

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

84 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

Related Questions

Performance issues with SendMessage in FixedUpdate using Raycast 0 Answers

how do i access an int var in a javascript and -= it from another javascript 1 Answer

Selecting a specific target with a 2D raycast 1 Answer

Decrease Health every 5 mins or Time script? 2 Answers

remove health from 1 gameobject at a time 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