- Home /
How to add an object to an array?
Hi, As I understood it there are 2 ways to create an array. At the normal array like in 'var blaa = new Array();' . But 1. It can't be displayed 2. Somebody said they're laggy. So I used this one 'var blaa :String[];'
But there it isn't possible to add another item to the list or to increase the length of this "array". Because then it says
MissingMethodException: Method not found: 'System.String[].Add'.
So my question:
How can I add an object to an array that was defined as var blaa :String[];
Answer by whydoidoit · Jul 11, 2012 at 09:57 AM
You want to do this:
import System.Collections.Generic;
var blaa = new List.<String>();
Lists are fast, type safe and support Add, Remove and Insert. They also use [ x ] to access values.
okay and how do I use them (script reference link I can't find anything about lists in javascript)
Here is the .NET reference for List - just bear in $$anonymous$$d to use in Unity Script you need to use the syntax:
List.<T>
A dot before the "less than" sign.
To be clear - they are a .NET feature that you can access from Unity Script or C# - there are many other useful things like this that you won't find in the normal script reference. Normal arrays [] are fine if the size won't change, use List if you need to insert or remove. The other damn useful one is Dictionary.