Assigning and reassigning a Unique ID

Hey all!

I’m having a problem getting my head around how I might assign a unique ID to an item for my inventory system.

There’s a lot of other stuff that’s completely irrelevant to what I need help with so I’ll make it as short as I can.

I need to check a List of integers (List inventoryList) for the integers that are present and add a new one to the list that isn’t already in there. It must be unique ie. no doubles.

Im struggling to get my head around how I might check each item in the list and add a new integer to the list in a sensible fashion. By that I mean a new integer that isn’t ridiculously high or even a negative integer.

My first thought was to make a foreach loop and find the lowest number (say ‘n’ for example) and create the new number at ‘n-1’ but I would have to make sure that ‘!n-1 =< 0’ etc. Then if it is 0 i would have to reverse the whole process and find the highest number- and that’s where I realise I am way over complicating it.

How would I find the lowest unused number in a List of integers?

Without just assigning it a higher value than the last that was added (obviously items in the inventory will be destroyed and removed from the list leaving unassigned numbers.)

I might just blow my unfocused and inexperienced brain out of my skull if I struggle with this much longer.

Cheers!

Halbera.

EDIT: Quick thought, would starting a foreach check at 0 work? If the current number is not found on the list then add it?

		public void AddItem(int _baseID){
			//assign a unique ID
			int _lowest = 1;
			int _newID;
			foreach (int _listItem in inventoryList){
				if (_listItem != _lowest){
					_newID = _lowest;
				}
				else {
					_lowest =+ 1;
				}
			}
			inventoryList.Add (_newID);
		}

Why not abstract this into a new DataType and keep track of an int that will promise uniqueness by incrementing it only when using it. Otherwise I think there is a constant time algorithm to determine what value is missing from a collection of values but that is pointless. Just count the adds and use the as the unique value. If all you care about is uniqueness you’ll get it that way very easily.

public class UniqueId {
  
     public int id { get; private set; }

     public UniqueId() {
         this.id = m_lastId++;
     }

     private static int m_lastId = 0;

}

EDIT: I know you asked for how to find the lowest unused integer, but I think it is better to just make sure integers never get reused in the first place.

For a completely scrambled list of integers, there’s no faster way than a for loop inspecting each and every integer to find the highest one. If your list gets large and you are doing this multiple times a frame, you could see a performance hit.

Unique ID?

Use Guid.

Guid.NewGuid is your friend. However, you might want to make sure that the id is assigned only once. For that you make a [SerializeField] string that’s assigned in Awake in an “(if not assigned) assign();” mechanisim.