- Home /
How do I find a gameObject's prefab parent?
Hello, I have a gameobject in my scene which is a deep child of an instantiated prefab. And I would like to know how to determine its prefab root/parent (in the scene) without having to resort to iterating up from parent to parent while comparing strings (which strikes me as very clumsy).
Obviously I cannot use transform.root since that would get me the topmost parent object, which could itself be a parent of my child object's prefab, not the prefab itself.
Oh and that should be at runtime too, so I can't use any editor function.
Since that sounds like a very simple thing I'm sure it's possible but I haven't found a way yet. Could anyone help please?
Answer by tswalk · Jun 20, 2014 at 07:13 AM
Perhaps you just need to add a component that stores that originating gameobject as a reference on its' instantiation. If you happen to know ahead of time which gameobject child is in the parents hierarchy....
Thank you, that would work of course. I was hoping for a more official (as in "built-in") solution for something so basic but since there doesn't seem to be any way to do that in Unity I'll go ahead and accept the answer... :)
I can relate.. there is a lot to be done when talking about game runtimes. I too am working on similar things, and finding it is a must to create different "state" managers for things I could easily handle in the editor (like via prefabutiliyy.xxx), but seems to be the nature of the how things are done here.
Answer by donnyjang · Jan 14 at 10:35 AM
I know this answer isn't directly related to the original question but I was trying figure out what the parent prefab was for a prefab variant in the editor itself and google took me here. Afterwards I figured it out so I thought I'd post my findings here just in case someone else does the same thing I did.
If you open the child prefab (As if you were going to edit the prefab) and then in the inspector, click the override dropdown in the top right. From there you should be able to see the parent prefab at the top as shown in the screenshot. If this isn't showing up for you it's possible that this was a feature added in a later version. I am running version 2020.3.25f1
Your answer
Follow this Question
Related Questions
Game Object seems empty after instantiation 1 Answer
How do I change a game object prefab tree to allow it to move in the wind? 1 Answer
Please manage the error 'Can not instantiate objects with a parent which is persistent.' 1 Answer
The variable othertransform of Prefab has not been assigned 2 Answers
Can you call a coroutine from a script on a prefab? 1 Answer