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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ryno9788 · Jun 14, 2015 at 01:29 AM · c#rigidbodydamagesendmessageexplosion

Damage sending with grenades.

Okay I've been trying to create a script that applies damage to rigid bodies through an explosion radius but the message I'm trying to send to another script doesn't seem to be sending. I'm not getting any error messages or anything the only thing I can think of is the the message has no where to go or the message is receiving but not applying. the action of actual force is working fine though.

This is the script I have so far.

using UnityEngine; using System.Collections;

public class Grenade : MonoBehaviour {

 public float radius = 5.0f,
 power = 10.0f,
 explosiveLift = 1.0f,
 explosiveDelay = 5.0f;

 void Update (){
     explosiveDelay -= Time.deltaTime;
     if (explosiveDelay <= 0){
         Vector3 grenadeOrigin = transform.position;
         Collider[] colliders = Physics.OverlapSphere (grenadeOrigin, radius);
         foreach (Collider hit in colliders){
             Rigidbody rb = hit.GetComponent<Rigidbody>();
             if (rb){
                 rb.AddExplosionForce(power, grenadeOrigin, radius, explosiveLift);
                 //This is where we apply force to our enemy and various other rigid bodies 
                 hit.SendMessageUpwards("Damager", 100, SendMessageOptions.DontRequireReceiver);
                 //This is where we apply damage to the enemy
                 Destroy(gameObject);
             }
         }
     }
 }

}

and this is my health script for my enemy

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour {
     public int maxHealth = 100;
     public int curHealth = 100;
 
     // Use this for initialization
     void Start () { }
     
     // Update is called once per frame
     void Update () {
         if (curHealth < 1) {
             Destroy (gameObject);
         }
     }
     void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "Bullet"){
             curHealth -= 20;
         }
     }
     // this is where our explosion should be inputing damage
     void Damager(int curDamage){
         curHealth -= curDamage;
     }
 }
 
 
Comment
Add comment · Show 3
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 savlon · Jun 14, 2015 at 03:46 AM 1
Share

Have you tried accessing the GameObject of the hit object before calling send message? hit.gameObject.Send$$anonymous$$essage ? Try just the basic send message first as well.

avatar image Mouton · Jun 14, 2015 at 06:46 AM 1
Share

Seems not to be the Send$$anonymous$$essageUpwards to be in fault. The only thing I can think of is that your destination script is not "Upward" the "hit" Collider. Be sure that you're not looking for a brother of a parent, because it won't receive the message.

 A (parent)
  _B (child of A, health script)
  _C (child of A)
   __D (child of C, hit collider)

Sending the message to D but 'B' holds your script won't work (only D, C and A will react)

avatar image ryno9788 · Jun 14, 2015 at 02:20 PM 0
Share

thanks savlon you where right. I didn't know that send$$anonymous$$essageUpwards was for child objects/parents so now its working

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by pako · Jun 14, 2015 at 12:22 PM

Most likely the EnemyHealth script is not directly on hit.gameobject or one of its ancestors, as @Mouton explained.

Unless you have a very specific reason to use SendMessageUpwards(), you should use GetComponent() or GetComponentInChildren() for better performance. So, the following should work (in place of the hit.SendMessageUpwards instruction):

 hit.transform.root.GetComponentInChildren<EnemyHealth>().Damager(100);


Edit: The Damager() method needs to be made public.

Comment
Add comment · Show 1 · 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 fafase · Jun 14, 2015 at 02:58 PM 0
Share

Just to add to that answer, the following is purely personal POV.

When you end up with:

  hit.transform.root.GetComponentInChildren<EnemyHealth>().Damager(100);

I consider you have a design problem. You are somewhere down in the hierarchy only to go up to search back down.

$$anonymous$$y solution would be to go up and find it there at the root using a controller that is aware of anything on that object.

 public class ItemController:$$anonymous$$onoBehaviour, IDamage{
       [SerializeField] private EnemyHealth health = null;
       public EnemyHealth Health { get { return health;}}
 } 

The method then goes on:

  hit.transform.root.GetComponent<EnemyHealth>().Health .Damager(100);


Best would even be an interface.

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

24 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

Related Questions

Explosion damage script only deals damage in the first explosion and none afterwards 0 Answers

Multiple Cars not working 1 Answer

Mesh Filter Cosmetic Damage 1 Answer

Distribute terrain in zones 3 Answers

Decrease ExplosionForce and damage continously by distance 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