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!

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.

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!