- Home /
Push Custom Class to Array (JavaScript)
Hi there,
I'm trying to push a custom class to an array and then output the value - but I'm having trouble. I can push the class object to the array without error, but when when I try to print 'EventMeta.EventObject.score' I get a NullRef. Code sample below, can anyone help?
private var CurrentTime : int = -1;
private var EventMeta = new Array();
class EventObject extends System.ValueType {
internal var type : String;
internal var time : int;
internal var score : int;
internal var height : int;
}
function PushToArray() {
var c = new EventObject();
CurrentTime++;
c.type = "Story";
c.time = CurrentTime;
c.score = Random.Range(0,1000000);
c.height = Random.Range(100,300);
EventMeta.push(c);
print(EventMeta.EventObject.score);
}
Answer by Seth-Bergman · Nov 08, 2012 at 07:29 AM
your line: print(EventMeta.EventObject.score);
is wrong, for an Array, we access the element of the Array by its index:
print(EventMeta[CurrentTime].score);
but I believe using Arrays is not good, you should instead use List, it is more efficient I think
Can you clarify if a List is more efficient even if the array size is always fixed?
Your answer
Follow this Question
Related Questions
Why Does Initialising Built-In Array of Classes Require Constructor? 1 Answer
Help with sorting values from a class 2 Answers
Pushing GameObject into JS Array returns NullReferenceException 1 Answer
Create an array of classes, of different type. 1 Answer
Reading and Storing External Data into Memory (From Text) 1 Answer