- Home /
Javascript Objects inside Arrays
I have searched through the forum and couldn't find an answer to this.
I want to declare a static array that contains objects. Outside of unity it was possible but here it's not working
My Code:
public static var menuMainList = [
{
text: 'Play',
callback: ''
}, {
text: 'Options',
callback:''
}, {
text: 'Exit',
callback:''
}
];
The error I get is unknown identifier 'text' and the same for the rest
no, this usually works outside of unity because { } are objects by default
the biggest problem is that UnityScript is not actually complete javascript
oh, then I will switch to another language then, I'll leave it open if someone has a better alternative
Answer by Blink123123 · Mar 17, 2014 at 06:05 PM
I found a solution:
public static var menuMainList = [
{
'text': 'Play',
'callback': ''
}, {
'text': 'Options',
'callback': ''
}, {
'text': 'Exit',
'callback': ''
}
];
to call something inside that array I have to call: menuMenuList[0]['text']
Answer by perchik · Mar 17, 2014 at 05:48 PM
If that's all your object is, I'd look into using Hashtables instead of an array of anonymous objects.
it doesn't seem expressive if I use it. I don't want to declare a variable and then add stuff to it.
If there is another way I missed and if you have the time convert the code I wrote into Hashtables please
Your answer
![](https://koobas.hobune.stream/wayback/20220613140457im_/https://answers.unity.com/themes/thub/images/avi.jpg)