Collision Detection for a Prefab?

How do i make sure that my destroy script destroys the proper prefab that it collides with? Currently, I’m currently trying to create a simple breakout game with a ball, a paddle, and some prefab bricks. When my sphere collides with a prefab brick (let’s call it blue brick prefab A), it ends up destroying another brick that was untouched (blue brick prefab B) instead.

function OnCollisionEnter(theCollision : Collision){
	if (theCollision.gameObject.name == "BlueBrickPrefab"){
		brickHit += 1;
		Destroy(GameObject.Find("BlueBrickPrefab"));
		Debug.Log("Blue");		
	}
}

I’m sure it has something to do with the fact that I created blue brick B before A in the Scene, but I’m not sure where to proceed. Any help would be appreciated. Thanks!

There is not need to do a:

Destroy(GameObject.Find("BlueBrickPrefab"));

That is going to return the first Object in the hierarchy it finds with that name. You already have the object stored in theCollision object, Try this:

function OnCollisionEnter(theCollision : Collision){
    if (theCollision.gameObject.name == "BlueBrickPrefab"){
       brickHit += 1;
       Destroy(theCollision.gameObject);
       Debug.Log("Blue");       
    }
}

try this one:

function OnCollisionEnter(theCollision : Collision){
    if (theCollision.gameObject.name == "BlueBrickPrefab"){
       brickHit += 1;
       Destroy(theCollision.gameObject);
       Debug.Log("Blue");       
    }
}

If the bricks have the same name, yeah, we can’t be sure of which of them the script will destroy. But note that your function receive as parameter the object with which the collision occurred. So, you could do something like this:

function OnCollisionEnter(theCollision : Collision){
    if (theCollision.gameObject.name == "BlueBrickPrefab"){
       brickHit += 1;
       Destroy(theCollision.gameObject);
       Debug.Log("Blue");       
    }
}