Multidimentional List - Class with different variable types

Hi
I have created a class that has a mix of variables including a gameObject, an int and a couple Booleans. I’ve then created a list as an instance of the class and I can appear to access an element of that list and assign it a value. I get no error messages until a launch the game and then I get an error. I am very new to lists, creating custom classes and so maybe this is something that just cannot be done or perhaps my code needs adjusting. Here is my code:

**this is the custom class
public class Target
{
public GameObject theTarget;
public int priority;
public bool detected;
public bool shortRange;
public bool isShielded; .

**this is the constructor although I am not really sure if it is needed (or how to use it)
public Target(GameObject newTarget, int newPriority, bool newScanned, bool newRange, bool newShield)
{
theTarget = newTarget;
priority = newPriority;
shortScanned = newScanned;
shortRange = newRange;
isShielded = newShield;

}
}

For the moment I am trying to do a very simple test to see if it works:
public GameObject testObject;

void Start ()
{
targets[0].theTarget = testObject;
Debug.Log(targets[0].theTarget);
}

On running the project I get out of range exception on the targets[0].theTarget = testObject; so my question is how do I assign a value to elements in the list? I’ve tried doing add(testObject) as well and it doesn’t seem to work. I’ve tried searching for other people who may have had this issue but I keep coming across people who are only using a list using one variable type which is not really what I am after. Maybe you cannot assign a value in the way I am trying to do it but strangely when I do targets[0].th it pops up with theTarget - so it recognises that the variable theTarget is part of the list targets which seems to indicate that its working?

Thanks in advance!
Dan

Figured it out now!

I added:

targets.Add( new Target {
theTarget = testObject
});

And now I can access the row at index 0 with targets[0].theTarget

It must need the initial add to create the row? .

No, the key to this is the “new” keyword to create a new instance of your Target class.