Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question UI reffrences link to new prefab

Discussion in 'Prefabs' started by cydonia, Dec 13, 2023.

  1. cydonia

    cydonia

    Joined:
    Apr 7, 2013
    Posts:
    11
    I have player prefabs for a project I'm working on, and it occurs to me I don't know how to link a new prefab to the UI automatically. Basically when a new prefab is loaded, say after a character death, all the UI references are cleared and the health bar, image toggles and all that no longer work because all the references to them in the inspector are empty.

    This is my first game project which incorporates a complex UI and this is a dumb issue that, although isn't a problem right now, will be a problem once I start work on the multiplayer aspect of the game that requires respawing and the like. So how do I have an asset automatically link all the UI image and slider references from the UI?

    The player health is linked to the UI so right now the ships explode upon loading as they aren't linked to the health bar so the script assumes health is zero. This is funny but not desirable.
     
  2. E2xp2lore

    E2xp2lore

    Joined:
    Nov 5, 2023
    Posts:
    7
    I'm guessing that when the player dies the scene reloads. When I had this problem I just stored things like health in a static variable, and had the UI update itself every time it's opened. However this is probably a bit risky because static variables can have some problems, in multiplayer too. There's probably a better way to do it but static variables aren't cleared when a scene is restarted.