unityscript class + array problem

I want to create a class and then make an array of its objects.

If I do it like this:

class strtest 
{var a:int;}
var  test:strtest[]; //and later: var test = new strtest[100];

Then I don’t know how to access it’s variables, since

test[5].a=3;

gives “a is not a member of strtest” or so. Which as I understand it means that some pointer goes nowhere and a different syntax is required. I believe the variable should be accessible this way since I can modify it from the inspector.

Now if instead I use

class strtest extends System.ValueType

I can access it as expected BUT it disappears in the inspector, which is annoying.

Solutions, anyone?

Side question: Am I supposed to be using classes at all when working with unityscript? (Or does that go against the concept of javascript)

Thanks in advance

When you

new MyType

(unless you do create it as a public variable visible in the inspector), that only allocates space for X variables of type ‘MyType’. You also have to create each one:

var test : TestClass[] = new TestClass[100];

for ( var ix : int = 0; ix < 100; ix++ ) {
  test[ix] = new TestClass();
}

This is a thing I still get caught up on once or twice a week, after YEARS of working with it. >_<

If that’s not your issue, try declaring “public var a” instead of just “var a”. I’m pretty sure the default is public, but maybe there’s a weird setup issue or maybe I have crazy memory.

If none of this is helpful, could you post the entire script, or make up an entire script that exhibits the problem?

Yes, you should definitely be using classes with Unityscript. I know that part of the answer’s right. :slight_smile:

Thank you your solution works.

So to make an array of classobjects you have to call each ones constructor indivdually after claiming its space.

class testclass  //define class testclass  
{var a:int;var b:int;}
var  object:testclass[]; //announce array of objects
var arraysize = 1000;

Awake()
{
  object=new testclass[arraysize]; //reserve space
  for (var i = arraysize; i--;) {object*=new testclass();} //call constructors (counting down was quicker in c++, it's probably the same in unityscript)*

}
As far as List() is concerned that’s no replacement for built in arrays, it’s a replacement for Array(). Built in arrays are fastest and not resizeable, List is somewhat slower(I’ve read about 50-60%, but not sure if thats correct) and resizeable, everything else should, according to public opinion, not be used (that includes java Array(), List is superior).
That said, List might save you running through empty entrys and might thus be faster.
@bunny: I don’t understand your comment. If we dont call
object=new testclass[arraysize];
how is unity supposed to know the size of the built in array?
Is there some way we can define an array of classobjects with less code and let unity do the work for us?