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

Question How to save player gameobject?

Discussion in 'Scripting' started by GameShotWD, Apr 13, 2024.

  1. GameShotWD

    GameShotWD

    Joined:
    Apr 1, 2021
    Posts:
    1
    I have a player gameobject with child objects (guns which player can pick up). I need to save my player with guns and load it when game starts. Only one solution that I could come up with is to give weapons an ID and then, when loading, give the player a weapon using this ID. Is there a better way to do it?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,528
    Nope, that's one of the better ways to do it.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,211
    Generally an unique ID is suggested as it means renaming things won't break save files.

    Same reason why Unity uses a GUID for every asset. It allows us to rename assets without breaking references.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,489
    It's one of the better approaches assuming that you're not going to have all kinds of variations for the weapon as it lets you save a weapon instance with just a handful of bytes.

    You don't need to assign a random number. Everyone has their own. :p

    https://forum.unity.com/members/zulo3d.12023212/
     
    Last edited: Apr 13, 2024
    CodeSmile likes this.