- Home /
Adding an object to an array of custom objects (JS)
Hello,
I've made a javascript class named tabXY with two variable inside (x and y).
I want to make an array of these objects. But I have some problem with it. If i do this :
var testList = new tabXY[100];
var testObject = tabXY();
testList.Add(testObject);
testList[0].x=125;
It return : 'Add' is not a member of 'tabXY[]'.
And if I do this :
var testList = new Array();
var testObject = tabXY();
testList.Add(testObject);
testList[0].x=125;
It return : 'x' is not a member of 'Object'.
Any idea? :p
Answer by Drakestar · Jun 02, 2012 at 03:33 PM
You make the first case work by using bracket notation. Built-in .net arrays don't have Add(). You make the second case work by casting the object back to tabXY. I'm not very firm on Java/UnityScript, though - there's probably a better way to deal with the type mismatch you're encountering.
#pragma strict
#pragma downcast
function Start ()
{
var testArray = new tabXY[10];
var testObject = new tabXY();
testArray[0] = testObject;
testArray[0].x = 100;
var testArray2 = new Array();
testArray2.Add(testObject);
var obj : tabXY = testArray2[0];
obj.x = 100;
}
Your answer
Follow this Question
Related Questions
Random an Array to push to another Array 0 Answers
Help with sorting values from a class 2 Answers
JS objects in Unityscript || BCE0048: Type 'Object' does not support slicing. 0 Answers
How to preserve array of custom objects in inspector? 0 Answers
Checking Player's distance to an Object 2 Answers