Recognizing Collisions with Instantiated Objects

var revolverMusketBall:GameObject;
static var health:int=100;
function OnCollisionEnter(theCollision : Collision) {
if(theCollision.gameObject == revolverMusketBall){
health -= 20;
Destroy (revolverMusketBall);
print (“Collision recognized.”);
}
}
The purpose of this script is to deduct health from the player when they are hit by the musket ball. The problem is the musket balls are instantiated from the original so they are not registering any collision with the player. How would I change this code to reflect colliding with INSTANTIATED musket balls?

Here’s your problem:

 if (theCollision.gameObject == revolverMusketBall)

You’re checking for reference equality against one object only. Surely there are other ways to check if the colliding object is a bullet? You could check for collisions against a particular layer that you’ve configured. You could check incoming colliders for a script component that you’ve added. You could check if the incoming collider’s name starts with some particular substring.

I’m not sure if that third option is the best, but it would be easy to set up, using the string class’s StartsWith() method:

if (theCollision.gameObject.name.StartsWith(revolverMusketBall.name))