Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Adding and Removing Objects from Lists

Discussion in 'Scripting' started by Cooo_oooper, Apr 20, 2023.

  1. Cooo_oooper

    Cooo_oooper

    Joined:
    Apr 20, 2023
    Posts:
    9
    Hi all,
    I have some trouble implementing something like Containers currently that can store so called "Garage Objects" in my game. In the prefab of the Container I already add one "GarageObject" to the containedGarageObjects List.
    I use a List<GarageObject> currently and add and delete the corresponding GarageObjects accordingly. But it seems like Lists in C# work quite differently than in Java. List.Count always returns the entire Lists size and not only the amount of elements that are not null which makes accessing the last element of the List quite tedious. I tried using List.TrimExcess() so the List is always at the correct size but that also does not work.
    What is the best/most elegant way to go about this in Unity?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    In Unity you can always prune a list of ALL Destroyed or null UnityEngine.Objects with:

    Code (csharp):
    1. myListOfGameObjects.RemoveAll( x => !x);
    Which is schmincy-schmancy way of saying "remove everything that tests false," which in tern makes use of the UnityEngine.Objects boolean overload.

    That won't work for lists of things that don't implement such an overload.

    Otherwise a null is just as valid as a GameObject as far as a List<T>() is concerned.

    Any reference type can be null, and hence can be stored in a List<T>()

    Also remember that Destroy() doesn't actually happen until end of frame. This means if you Destroy() something and immediately ask "Is this destroyed?" the answer will be "Nope."
     
    seejayjames likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    BTW if you're making a stack of these things and you want to always add/remove from the top of the stack, there is actually precisely such a thing for you, the
    Stack<T>()
    type.
     
  4. Cooo_oooper

    Cooo_oooper

    Joined:
    Apr 20, 2023
    Posts:
    9
    Thanks a lot for the reply! I thought about using a Stack but found it might restrict me later if I want to have Containers where I not only want to remove the last object but also maybe something from the middle or something like that.
    I will try out your other suggestion and report back.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    If you wanna remove from anywhere, definitely use a List<T>()

    Remember that you can call .Remove() to remove a particular item, but be careful: if you have TWO of the same item in the list and you say .Remove(), it only removes the FIRST one it finds. This is all covered in the docs for List<T>()

    Otherwise thare is also .RemoveAt() which accepts an integer saying "remove this element regardless of what it is." It is subject to all the same indexing rules: you MUST specify an existing item.
     
    Hefty_Chomnker likes this.
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Can you show us an example of how your list is expected to work?
    Because you got me confused here: List.size in Java works exactly the same as List.Count in C#.