Is there a possibility to use List.Add in the Update function just once so that it’s not filling the list over and over again with the same Object? Is this only solvable with a bool or is there ‘simpler’ way with just a command? Thx!
Using some type of boolean check is the simplest way to do what you are asking. You could make a custom function like this to handle adding an item only when a list doesn’t contain it:
void AddObjectToList(Object obj, List<Object> myList)
{
if(!myList.Contains(obj))
myList.Add(obj);
}
With the function above, the Object “obj” can only be added to the list once. So you can conceivably call this function in Update() without having to worry about an Object being added twice. However, the look-up necessary for defining what “obj” is within the Update() function would be a waste of overhead since the check for it would be done every frame.
The above solution would work, but perhaps a better explanation of what you are trying to do would facilitate a better solution overall.
If you want your code to add only one item on that list, i believe the Start method is the appropriate place. If you really need to put it in Update, there isn’t a “simpler” way than a bool statement:
if (list.Count == 0)
list.Add(item);