Trying to figure out the best way to have a series of ints that are dynamically numbered in sequence

I want to have a set of named ints (not a list, because adding ints to a list and then changing the value of the item in the list doesn’t change the original) that, when the appropriate function is triggered, will be given a sequential numbering (0, 1, 2, 3, etc). Of course, that’s easy enough to just do by hand, but what I want is to be able to add new variables or remove existing ones, and still have them be sequentially numbered. So like if the set is “ExampleIntBlue, ExampleIntRed, ExampleIntYellow;” then ExampleIntBlue would be 0, ExampleIntRed would be 1, and ExampleIntYellow would be 2. Then if I added ExampleIntGreen in between red and yellow (and then triggered the numbering function again), I would want ExampleIntGreen to be 2 and ExampleIntYellow to be 3. If I then removed ExampleIntRed and ran the function again, green would be 1 and yellow would be 2.

Is there any way to do that, that would be easier than just renumbering them manually every time? (The list won’t be super-long, but it might be like 20 or 30 items.) The only thing I can think of is doing them as GameObjects with a string to define the name of the resulting int, and then having the parent of those objects run through the loop of all the children (since doing that goes in the order they’re listed in the hierarchy). But I figure there’s gotta be an easier way to do it than that.

I think this would be a good place to use the insert method of List. List.Insert(index, item); But if Count == Capacity I believe the item gets added to the end of the list. If the list always stays the same size, just remove the item that needs the index change and then insert it to the desired index.

List.Insert(3, “green”);

This part of your question

(not a list, because adding ints to a list and then changing the value of the item in the list doesn’t change the original)

Although this is true for an integer, that has nothing to do with the list. If you place a ‘reference’ type item in the list (a.k.a. a class) then modifying it will change the original. If you place a ‘value’ type in the list (a.k.a. struct) (int, bool, float, etc. are all value types) then modifying it will only modify the value in said list. See also this answer on stackoverflow

It doesn’t matter where you store the value or reference (variable, Array, List, LinkedList, etc.) the above statement holds. Meaning the ‘list’ has nothing to do with the fact whether it updates the original or not, the original type decides that.

Now i’m not sure what your intent is here with these integers. And why you put then in a list in the first place, and then still want to acces them outside your list. So i’ll just write a very general ‘wrap it in a class’ solution here so you can change the value in a list while at the same time changing the original…

public class Example
	public int Value { get; set; }

	public Example(int value)
		Value = value;

Usage of this class would be like this:

List<Example> test = new List<Example>();

Example test1 = new Example(0);
Example test2 = new Example(1);


test[0].Value = 10;
Debug.Log(test1.Value); // Will return 10 instead of 0