- Home /
How do I create and loop through a completely generic list in javascript?
I don't really understand generic list syntax. I'm used to working with arrays and objects. All I want to do is initialize and declare and object at the top of the script like this:
var walls = {
{
"name": "East",
"height": 0,
"width": 0,
},
{
"name": "West",
"height": 0,
"width": 0,
},
{
"name": "North",
"height": 0,
"width": 0,
},
{
"name": "South",
"height": 0,
"width": 0,
}
};
And then loop through it, using the implied numeric index:
for (var i = 0; i < walls.length; i++)
{
Debug.Log(walls[i].name); //etc
}
But apparently #pragma strict doesn't like arrays or objects, so how can I use lists to accomplish what I want?
Answer by Eric5h5 · May 21, 2013 at 08:35 AM
Unity doesn't use Javascript; it uses a custom language called Unityscript (even if the docs refer to it as "Javascript", it's not). Therefore you can't really use much "actual" Javascript syntax for anything. Think of it as being like a cross between ActionScript 3 and C#. As such, it looks like you'd want to use classes, which work exactly like they do in ActionScript 3.
class Wall {
var name : String;
var height : int;
var width : int;
}
Your answer
Follow this Question
Related Questions
How to Manually write a 2 dimensional array in javascript 0 Answers
Get a List of the declared objects in a class 1 Answer
Converting JS Array to Generic List 2 Answers
Serialization Error 1 Answer