- Home /
The question is answered, right answer was accepted
How would I find the prefab of a gameObject?
Essentially, I created a Dictionary of GameObjects and integers. The GameObjects are prefabs, and the integers are how many instances of them are in the scene.
I have 'buildings' and, at the start, a script goes through all the buildings and adds them to the Dictionary. The plan is that if the dictionary does not include that prefab it will get added, otherwise it will increase the instanceCount by 1.
My question is how do get the prefab GameObject? Getting just the GameObject won't help because they all have different names, but a reasonably extensive internet search did not come up with any ideas other than PrefabUtility.GetPrefabObject(), which is obsolete.
Thank you very much for your help! If you need clarification on any points, please ask!
There may be a better way, but I personally would just tag all the prefabs and do something like: GameObject.CompareTag("house1")
And there's another similar answer I found that implements a solution I would have used too. It either adds the item to the dictionary, or increases the count.
Yeah, their method was what I was doing, but I still have to get the GameObject to store. Thanks for the tag suggestion, but then each instance would also have the tag unless I went through them individually. Alternatively, I could create a different tag for each prefab and its instances and compare. Thanks.
Why do you need to differentiate between a prefab and its instances?
It looked like you were just filling an Dictionary with unique GameObjects and the number of times they appear in the scene?
If you tag all the prefabs with house1,house2,house4,bakery,bank etc - you'll get the results you need?
I must be misunderstanding the requirement because I think this gives you everything you need?
Answer by Ironstone · Apr 21, 2019 at 01:24 PM
Okay, I found a way to do it. I simply Serialized a name for each type of building's prefab and stored the name as it was common to all the same buildings. Thanks for all the help!
Answer by DiegoSLTS · Apr 20, 2019 at 04:37 PM
https://docs.unity3d.com/ScriptReference/PrefabUtility.GetCorrespondingObjectFromSource.html
According to the docs: "Use this function to get a Prefab Asset object the source was instantiated from".
Also note that with the new nested prefab workflow the "Prefab Asset" of an object in the hierarchy is not as clear as before. One object could be an instance of a certain prefab ("Nearest root") which is nested inside another prefab ("Outermost root"). You might want to check the other new methods on PrefabUtility just in case.
Thank you very much! Somewhere I found said that the entire PrefabUtility thing was removed, but luckily not! Edit: Unfortunately, this does not seem to work; it just returns null all the time. I am checking all the PrefabUtility methods, but GetOutermostPrefabInstanceRoot() will not work either, nor a combination. I'll keep searching, and thanks.
Follow this Question
Related Questions
Make a gameobject when you click 2 Answers
How to tell at runtime if a GameObject is a prefab 3 Answers
Distribute terrain in zones 3 Answers
Problem to play animation in C# 5 Answers
Multiple Cars not working 1 Answer