3 ms·
The best way to communicate across hierarchies is to set the other GameObject/Component that you need to communicate with as a public field and then manually se
by Jorge1o1 6y ago
The best way to communicate across hierarchies is to set the other GameObject/Component that you need to communicate with as a public field and then manually set that GameObject via the Editor so now the UI has a reference to the Player’s Gun’s AmmoBehaviour. You can then use Events and EventHandlers to have the UI subscribe to every shot and now it can show how much ammo is left in the gun.
This can get kind of tricky if you use a lot of procedural generation or prefabs where for some reason you can’t immediately set the reference to that other GameObject (for example, the player has no gun yet). In those cases you should try to set the reference programatically, such as when the Player picks up the gun. You might have a reference from the Player to the HUD, and when the player picks up the gun you set the Gun’s UI reference via some SetUiObject(GameObject ui)