- Home /
Why cant i do this with an array?
Im just starting to learn how to use arrays and classes and im wondering how i can get this to work can someone help me? Thanks!
TestArray
var people = 10;
var personArray : myPerson [];
class myPerson
{
}
function Start ()
{
personArray = new myPerson [people];
}
AddToArray
var testInventoryObject : Transform;
class myPerson
{
var myName = "Brandon";
}
function Update ()
{
if (Input.GetKeyDown("e"))
{
testInventoryObject.GetComponent(TestArray).personArray[0] = myPerson;
}
}
Can someone tell me how i can make this work?
Answer by Benproductions1 · Feb 20, 2013 at 04:23 AM
In Unity, Arrays are very different from built in arrays
A built in array can only contain 1 type of object, but is faster
var lolies:lol[];
You cant change it without creating a new one, it's non dynamic
A Array is what your used to and is defined like this:
var lolies:Array = [lol]
They are slower, but dynamic
Hope this helps, Benproductions1
So can you give me an example of a script like i should be using? Thanks.
You should never use the Array class. Use a generic List if the array should be dynamically sized. Also, what you're talking about is a built-in array, not a list.
If you looking for a list (generic list) heres a good link:: http://answers.unity3d.com/questions/166789/iterate-through-generic-list.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613093028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to access a variable within a multidimensional array of class 2 Answers
Making a grid.Help me understand. 1 Answer
IndexOutOfRangeException: Array index is out of range 0 Answers
Set default length for an array of elements of a custom class in inspector 0 Answers