Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding objects to list

Discussion in 'Scripting' started by Truls_, Jan 17, 2022.

  1. Truls_

    Truls_

    Joined:
    Dec 4, 2016
    Posts:
    104
    I'm trying to make a inventory system, and I have objects that I can interact with (pick up), and I have tried to add the gameobject to a List<Gameobject>, which works, but then I destroy the gameobject because I don't want it in my scene anymore obviously, because it's in the inventory, but then in the list it appears as "missing" because I destroyed the reference object.. Does anyone know of a good way to add it to the list and remove it from the scene? All the items carry the same script but with different "stats" so I need an exact copy of the item I interact with.

    Any help is appreciated :)
     
  2. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Maybe you just can use SetActive(false).
    And maybe it helps if you upload your code...
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Inventories (and shop systems) are fairly tricky hairy beasts. They contain elements of:

    - a database of items that you can possess
    - a database of the items you actually possess
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow)
    - interaction with items in the inventory
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items?
    - can users substantially modify items with other things like spells, gems, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Or... do like I like to do: just jump in and make it up as you go. It is SOFT-ware after all... evolve it as you go! :)

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - Star Manta on the Unity3D forums
     
    glgamesforfun likes this.