- Home /
how do I set up an array of class vars
I set up a class like this
class Level extends System.ValueType{ //fields var levelNo :int; var levelPads :int; // number af pads for level var levelName :String; var levelStatus :int; // editing, testing, done var levelExtra :String; // extras
//Constructor public function SetLevel(lvn:int,lvp:int,lNm:String,lSt:int,lEx:String){ this.levelNo = lvn;this.levelPads = lvp; this.levelName = lNm;this.levelStatus = lSt; this.levelExtra = lEx; }
}
then I declare the var like this
static var CustomLevels :Level = new Level();
Then i want this to be a dynamic array of Levels so I can use
CustomLevels[1].levelName = "Beginers Luck"
I will need to add and subtract to this array so I dont want a builtin array. How can I set this up? I have been getting splicing errors when I add brackets.
EDIT:
That fixes casting as Value type error but I still get 'levelName' is not a member of 'Object'.
static var CustomLevels = new Array();
CustomLevels.length = 1;
CustomLevels[0] = new Level() as Level;// -> Fixed this ERROR
CustomLevels[0] = InitLevel;
print(CustomLevels[0].levelName);// -> ERROR
That's not a class, that's a struct. This sort of thing should probably be a class, so get rid of the "extends System.ValueType".
I made a very small change that should solve your issues.
Answer by Peter G · May 09, 2011 at 04:16 AM
You cannot convert a single instance to an array just by adding brackets like you tried to do hence the error.
I would try a List<>();
import System.Collections.Generic;
static var CustomLevels = List.<Level>();
then you can call Add() and Remove() to add and subtract levels as need be.
using pragma strict because of porting to android, unity doesnt like the (). static var CustomLevels : List.; compiles but in run time i get Object reference not set to an instance of an object errors when i reference CustomLevels print(CustomLevels.Count);
Sorry, it should be an "=" not a ":" in the variable declaration. That should solve the "()"'s problem.
Alright, you've got me off and running with Lists, thank you.
Your answer
Follow this Question
Related Questions
Prepopulate Class Array 0 Answers
Cannot edit values of Class when created using .JSON file 1 Answer
Does XML Serialization Support "Class Object" Arrays? 1 Answer
C# Adding to an Array 1 Answer
Resizable Array of Classes? 2 Answers