- Home /
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[i] = 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.< $$anonymous$$yClass> 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.
Answer by fred_gds · Mar 27, 2013 at 09:46 PM
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.
Your answer
Follow this Question
Related Questions
Add custom declared variables in a Array 1 Answer
C# Adding to an Array 1 Answer
Help with sorting values from a class 2 Answers
Custom Sort class array error if i use #pragma 2 Answers
Script wont read array 2 Answers