- Home /
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;
}
}
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.
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)
thanks savlon you where right. I didn't know that send$$anonymous$$essageUpwards was for child objects/parents so now its working
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.
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.