Destroy Projectile on Impact?

I’m a noob making a disc golf game. I think I’d have trouble getting the discs to stick in the chains on impact, so I’d like to just destroy the discs on impact with the chains. The chains have a sphere collider and it’s marked as a trigger. I also have an explosion set to detect collision. The collision is working.

However, I’m having a hard time figuring out how to simply destroy the discs. All discs are tagged as “discs”. They’re defined in a different script. Here is my code:

var explosion : Transform;

function OnTriggerEnter(hit : Collider)

{
if(hit.gameObject.tag == “discs”)
{

Destroy gameObject.tag == "discs";
		
	var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);	
		
				
			
					StrokeCount.STROKES = 0;
					StrokeCount.BASKETHITS += 1;
}

}

Trust me, I understand that I’m noob. I appreciate any help that’s offered.

Hi there and welcome. The Unity Answers forum is more for focused Q&A on Unity as opposed to general discussion topic so you may want to look at the Unity Community forums for questions such as this. Looking at your code, I see that you have a line which reads:

Destroy gameObject.tag == "discs";

I am actually surprised this even compiles. The syntax for the Destroy command can be found here:

http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html

As you will see, it expects an Object as a parameter and this is commonly an instance of GameObject to be destroyed. In your code, you are passing in:

gameObject.tag == "discs"

Which is a “boolean” (a true/false value) and most certainly not an object. As such, your discs are not disappearing.

From what I understand this destroying projectiles on impact is very similar behaviour to shooting rockets that explode when colliding, right?

http://unity3d.com/support/resources/tutorials/fpstutorial.html

I don’t know why this tutorial is hided for people. It’s pretty old but when you’re searching for some ideas it will do…
And some colliding/destroying things you can also find in… Lerpz 3D platform tutorial:

I found also this:

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

And I guess that will be the best resource for your problems. It’s overcommented so noone should have problems with understanding and implementing this in their projects.

If your original script was attached to the disc, it should be:

var explosion : Transform;

function OnTriggerEnter(hit : Collider){
  if (hit.tag == "chain") { // if hit a chain object...
    Destroy(gameObject); // destroy himself...
    // and instantiate the explosion in its place:
    var exp = Instantiate(explosion, transform.position, Quaternion.identity);  
    StrokeCount.STROKES = 0;
    StrokeCount.BASKETHITS += 1;
  }
}

Your second script will not work unless you uncheck Is Trigger in the chain collider: OnCollision events only occur when the rigidbody hits a regular collider - OnTrigger events are reported when the collider has Is Trigger checked.

Your disc script could be something like this:

var explosion : Transform;

function OnCollisionEnter(col : Collision){
  if (col.gameObject.tag == "chain") { // if hit a chain... 
    Destroy(gameObject); // destroy itself
    // instantiate the explosion at its own position:
    var exp = Instantiate(explosion, transform.position, Quaternion.identity);  
    StrokeCount.STROKES = 0;
    StrokeCount.BASKETHITS += 1;
  }
}