Use on collision enter to send message to what we've collided with?

Hi,

I’m writing an “arrow” stylee script, and I thought I’d use something like-

On collision, getcomponent “Health” and deduct x from it.

However, nothing I’ve written has worked.

I dont want to use the tag system (Which I wrote anyway, and worked fine…grumble) because it doesn’t seem to offer as much control, or it may even break if I call it too frequently.

Suggestions, ideas?

If there is some Health component attached to the colliding object, then getting it should work. Just test that it’s not null, otherwise you get an exception when trying to deduct x from something without a Health component.

Interesting…I think thats what I did…

When I get home from work I’ll upload the code, see if anyone else can spot my mistake…

Hrmm, still can’t get it to work…

Here’s two little snippets of code-

In the arrows code-

void OnCollisionEnter(Collision hit) {

Healthconnector hc = gameObject.GetComponent hit.Transform.

}

(I know it’s missing a tad, I’ve been reworking it to find a solution, which I havnt managed)

And here’s the Healthconnector script chunk-

using UnityEngine;
using System.Collections;

public class Healthconnector : MonoBehaviour {

public int Health = 100;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(Health <= 0)
Destroy (gameObject);
}

public void AdjustCurrentHealth(int adj) {
Health += adj;

Can anyone reccomend a change to be made?

Hmm… I am suprised no one actually looked at the title… OnCollisionEnter… SendMessage. Seems very obvious.

Your missing the SendMessage part… and oddly enough, you would have found just that in the documentation.

Basics: A bullet hits a person, it starts the OnCollisionEnter event, the onCollisionEnter event sends a message to the hit object that says “ApplyDamage” and has an amount. It also says it doesn’t really require anyone to listen to it. Then destroys the bullet.

The object that it hit, has a health recorder. This has a function “ApplyDamage” Since it has this function and it fits ApplyDamage(integer) it applies some damage to the health. It then checks to see if the health at or below zero, then destroys the object if it is.

//BulletScript.cs
using UnityEngine;
using System.Collections;

public class BulletScript : MonoBehaviour {
	void OnCollisionEnter(Collision hit) {
		hit.gameObject.SendMessage ("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver);
		Destroy(gameObject);
	}
}
//Healthconnector.cs
using UnityEngine;
using System.Collections;

public class Healthconnector : MonoBehaviour {
	public int Health = 100;

	// Use this for initialization
	void Start () {
	}

	// Update is called once per frame
	void Update () {
	}

	// non returning method for applying damage
	public void ApplyDamage(int adj) {
		Health -= adj;
		if(Health <= 0)
			Destroy (gameObject);
	}
}

Also, when posting code, please use code tags. It will keep the formatting.

Thanks, worked very well! And with a little alteration, I’ve got it working perfectly for my game, thanks very much!