Check if instance is child of certain prefab

(Apologies if I’m posting this in the wrong forum!)
Hello,
Development on my game is going smoothly, but I ran into the following issue: I need to know whether the instantiated object is a child of another prefab.

So, to make this a bit more clear: I have a prefab called “Bullet Parent”, with a child called “Splattershot Bullet”. I instantiate this prefab during runtime. Upon colliding I need to be able to check whether the object I collided with is a child of “Bullet Parent”. It seems when I instantiate the Splattershot Bullet on its own, the parents gets lost.

This is the code I tried:

if (Physics.Raycast(new Ray(RayStart, RayDirection), out hit, _camDist)) {
if (PrefabUtility.GetPrefabObject((Object) hit.transform.parent) != _bullet) { //_bullet is the bullet parent I had assigned to the gameObject in the inspector
//Always triggers }}

I’m unsure from your description, but I think you’re going about it the wrong way. If you want to have splattershot bullets avoid colliding with other splattershot bullets, you should do it one of the following ways:

  • Add them to a “Bullet” physics layer and have it not collide with itself.
  • Give them a tag and on collide, check gameObject.tag and throw the collision away if it is another bullet.
  • Only do something if the collider hit is of the correct type, using “if (hit.gameObject.GetComponent() != null)”

Using PrefabUtility won’t work at runtime, it is only used in the editor. Hope this helps.