using UnityEngine;
using System.Collections;
public class testBallSettings : MonoBehaviour {
public float speed = 10.0F;
public float Damage = 50.0F;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(0, speed * Time.deltaTime, 0);
}
void OnTriggerEnter2D (Collider2D hit)
{
if (hit.gameObject.tag == "enemy") {
hit.gameObject.SendMessage("ApplyDamage", Damage);
Destroy (gameObject);
}
}
}
The code is attached to a bullet. and when the bullet hits a target with the tag enemy, it shall reducet the target health.
But right now, its reducing every one who has the tag enemy and not just the one who got hitted with the collider.
@MrJohny_1 I had no idea that it worked this way, if indeed it does. The documentation states that a GameObject.SendMessage() should only go to all components on that specific GameObject with the matching function signature, so I’m not sure that this is the problem.
@MG Are you sure that the colliders aren’t so large that all of them are getting hit simultaneously? Try running .SendMessage() on a specific component instead of the entire GameObject, and see if that makes a difference. Also check the size of your colliders and make sure they more-or-less match up with the size of the objects they’re being used for.
Did you try running the SendMessage on the component instead of the whole GameObject? You can do this by saying hit.gameObject.GetComponent<SomeScriptName>().SendMessage("ApplyDamage", Damage);
How are the different objects with hit colliders being created? Are they already in the scene or are they being instantiated from a prefab? I’m really confused by this, because my understanding of SendMessage() was that it was limited to the scope of the GameObject.
I really enjoy the GameObject.SendMessage() method because you can have two different functions on different scripts that will both react to the same event, quickly and easily with no fuss. Using the Component.SendMessage() method, on the other hand, completely defeats this purpose and is absolutely no better than calling the function directly. That said, it didn’t even occur to me to ditch the SendMessage method, because I’m extremely curious as to why it’s not operating like it should. If he goes another route and that fixes it, it may hide a larger methodology problem here (also, more importantly, my curiosity will never be sated!).
You misunderstand, I don’t use it at all- I have a type-safe event “messaging” system that I built, which is what I use when this kind of functionality is necessary. That said, it usually isn’t necessary since Unity’s so good at making just about everything accessible from just about everywhere with minimal effort without it (it’s still nice for decoupling though). I’m still extremely curious why it isn’t working as it’s supposed to.
and just keep Health -= damage in there, and use the method I said. Don’t forget to change void ApplyDamage (…) to public void ApplyDamage(…).
Then just have this instead of SendMessage:
var hitObject = hit.gameObject.GetComponent<moveCar>();
if(hitObject != null)
hitObject.ApplyDamage(Damage);
Anyway, weather or not you use SendMessage is not the problem. The reason that the health of every movecar object goes down when you apply damage is because your Health variable is static.
If a variable is static, it belongs to the class, not instances of that class.
Oh, I didn’t see that it was static! Well now you know the problem, you can revert everything back to the way it was, including the SendMessage. But just make the health variable not static.
That wasn’t a response to you. MG had asked “I’m not writing in javascipt. How do I write that in C#?” but then edited his post quickly enough that it doesn’t show a ‘last edited’ tag.