Several identical prefabs and interaction with raycast

Hello.
I have several identical prefabs (different name but identical tag).
In each prefab I have a particle, an object and another object which I use as a collider.

My raycast (which is on another prefab) touches the prefab colliders well (I get their names well).

How do I make sure that when the raycast hits prefab2, I activate the particle, then I destroy the object, then I destroy prefab2?

Thank you

I assume you mean you have several gameObjects in your scene that are based on prefabs. When your raycast hits a gameobject, you can get the gameobject it hits and then you just need to trigger the particle and then destroy the object.

Note, you don’t destroy prefabs, those are in your project folder.

I would honestly probably just have a script on the object that would handle the playing of the particle and then the destruction as needed and trigger that.

Thanks for your help.

The problem I have is that I have 3 GameObjects called “Wall1”, “Wall2” and “Wall3” which are all identical. In these gameobjects I have a child called “MyParticle” and another who is a gameobject called “MyCube”.

My Raycast targets “MyCube” from “Wall2” (it’s wanted) and I would like to destroy “MyCube” and activate “MyParticle” which are in “Wall2”, and to do that I do a “Find”, but it activates the particle of “Wall1” or destroy the “MyCube” of “Wall1”!

I tried :
GameObject.Find
transform.parent.Find
But I can’t precisely target “MyParticle” which is in “Wall2”.

I don’t know if I’m clear;)

You’re clear, but I don’t see the problem. You said you’re using a raycast. You need to grab the gameobject the raycast hit. Then, let’s say you have a script on that gameobject called DestroyObject. This script would have a reference to the particle and whatever else it needs to handle destroying itself.

So you grab that script which has a Method on it, ExecuteDestruction and you call that method which then handles your particle and destroying itself.

There should be no need for GameObject.Find, transform.parent.Find, any of that.

In the document you can see you can get the transform back of the object hit. From there you can get pretty much anything else from the hit object.

Thank you for the info. I’ll watch.
I still succeeded using “Find” but I had a hard time!

I would suggest you redo it without Find as you could run into future issues and then you’ll forget what you did and not know what to do to fix it.