Unity 5 - How to tell two prefabs apart?
So I have this issue. I'm making a splitscreen game where each player has a UI prefab. In code, I need to tell these prefabs apart in code, but I don't know how.
For example, the prefab has an embedded GameObject called "HUD Menu". Each time this prefab is instantiated, I need to use GameObject.Find("HUD Menu") to find HUD Menu. While the first instance of the prefab works fine, since there's only one instance of HUD Menu, the second doesn't because now the code is looking through two identical game objects with no way of differentiating them.
How can I make it so that I can still use the one prefab for all players, but still tell which prefab is which?
Answer by tanoshimi · Nov 06, 2015 at 02:30 PM
Give them different names after instantiating? i.e.
GameObject newHud = Instantiate(yourPrefab,...);
newHud.name = "HUD Menu 1";
GameObject nextNewHud = Instantiate(yourPrefab,...);
nextNewHud.name = "HUD Menu 2";
This worked out in the end. I had a similar solution using tags, but using names is significantly easier. Thanks for the help. I know the question may seem to have an easy answer, but I'm not running on much sleep, so I appreciate the help anyways.