class array removeAt

Hey,

I created a custom class in unityscript. Like that:

class MyClass
{
var one : int;
var two : int;
}

then I created a variable

var myClassVariable : MyClass = new MyClass[5];
function Start()
{
for(var i : int = 0; i < myClassVariable.length; i++)
{
myClassVariable = new MyClass
}
}
so and then later on I wanted to delete myClassVariable[0] and to add a new value to the end of the variable like that
myClassVariable.RemoveAt(0);
myClassVariable.Add(myValueToAdd);
But unity tells me that RemoveAt and Add aren’t part of MyClass.
Could anyone give me either a method which allows me to remove the first value of an array and to add a new value at the end (so something similar to RemoveAt and Add) or explain to me how to get those functions to work on a custom class?
Best regards,
Fred

You should use a List.< MyClass> if you want to add and remove them. See this

Sorry to ask. But what is the differnce between a List. and an array? and the link you porvided doesn't handle this subject

Lists can be added to and removed from at runtime. Arrays cannot. Otherwise the semantics [] are the same, but Length becomes Count.

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

1 Answer

1

I tried the list thing but I already have too much stuff depending on that array and I don’t want to rewrite everything.
So I wanted to write smth like this:

if(GUI.Button …)
{
changeClass();
}
function changeClass()
{
myClass[0] = myClass[1];
myClass[1] = myClass[2];
myClass[2] = myClass[3];
myClass[3] = myClass[4];

myClass[4].amount = number;

}

so the button can only be pressed once for each car (its from a car game).

Lets start as I have it. So at the beginning all values equal 0 and the value number changes everytime the script gets called.

Then I press the button once and the function gets called. Then it returns me the same value for myClass[3] and myClass[4].+

And if the function gets called again myClass[3], myClass[4] and myClass[2] equal the same value and so on. But I can’t find the reason why it’s doing this as the script doesn’t allow it.