Destroying Object when collides with wall

I want my bullet (made form particles) to destroy itself with it collides with a wall or another person…
I created my level in Maya and exported it and generated colliders…
I then put a particle collider on my particle system (bullet)
THEN, i attached the below script to the bullet but for some reason, it wont detect any collision…any ideas on how i can destroy the object when it hits the walls…then i can implement it being destroyed with it hits an enemy ect…

function OnControllerColliderHit(hit: ControllerColliderHit)
{
	Debug.Log("hit wall");
	
	if(hit.collider.gameObject.tag == "level")
	{
		Destroy(GameObject.FindWithTag("bullet"));
	}
}

If you really want to use particles for the bullet, then you probably want OnParticleCollision().

If this isn’t the problem, then it’s possible that your bullet is traveling too fast. Experiment with lowering the speed first, and see if that’s the problem. Try using a simple mesh collider instead of particles, unless you really have a reason to be using particles for something like a bullet. From the way you’re destroying the object, it sounds like you can only ever have one bullet active at a time anyway, so you don’t gain much from using particles.

OnControllerColliderHit is used when you have a CharacterConroller on your object. You probably want to use OnCollisionEnter instead. And collisions are only detected if one of the objects (the bullet in this case) has a non-kinematic rigidbody attached.

Here is the page with examples for OnCollisionEnter.

im using particles because when i fire, i want the bullets to have alittle glow to them , like a plasma gun…unless there is a way to do this using a simple sphere or cube…?

And i do think my particles are moving too fast, how can i fix this issue so it collides no matter how fast the particle is traveling?

Create a simple sphere/cube prefab for your projectile, and attach a light to it. Attach a script to it that makes it travel in a straight line by increasing it’s local translation in the “forward” direction. Attach your collision script to the prefab. Instantiate this when you fire, positioned at the end of the gun.

You’re always going to have collision issues if your projectile is moving incredibly fast, and there are a number of ways to “solve” this. I doubt this if your problem here, so worry about that later.

i used your technique and i can work with that…i tried using this

function OnCollisionEnter(collision : Collision)
{
	Debug.Log("hit wall");
	Destroy(GameObject.FindWithTag("bullet"));
}

but sometimes the bullets get destroyed…some don’t…it acts very weird…any ideas?

anyone please?

The reason why is because you are looking for any object with the tag “bullet” and it only grabs the first one it see’s in memory. Without coding the answer for you I will try and help.

In order for OnCollisionEnter to work you need a rigidbody on one of the objects involved. Or you can use OnParticleEnter like the other person said.

Destroy(gameObject);

Will destroy whatever bullet the script is on. Everytime you fire a bullet, there is a GameObject with a Transform created. Inside that GameObject you have hierarchy system. your script is under the GameObject and Transform. If you have three objects in the sceen, you have three GameObjects, Transforms, and BulletScripts. If you use Destroy(this) it will destroy the script on the GameObject. this is a pointer in memory that lets the computer know what “Object” you are talking about. Destroy(gameObject); will locate the highest member of the hierarchy that the script is on, and delete it, thereby deleting everything, the GameObject, Transform, Script, and Particle.

That being said, if you have three bullets on screen, the GameObjects all have a “tag” of “bullet”. GameObject.FindWithTag(“bullet”) will return a random GameObject “bullet” and destroy it, it won’t always be the one that collided. I hope that helps. Alot more information than you needed, but I had trouble figuring out that when I first started so I figured I would share.