- Home /
How do I make an array of vectors?
This is for javascript. Pretty simple question. I want an array to store a series of vectors, but I don't know the syntax for creating and adding new vectors to the array. Thanks.
Answer by landon912 · Dec 10, 2012 at 12:31 AM
There are two types of arrays(there are more but lets focus on these two), the first is a native .NET array. The main differences are that it is static, meaning that the length has to be preset. In order to change the length it has to be remade. Another difference is that the built-in .NET arrays can be edited and set in the inspector. While remaking can be a pain for the CPU, if you arent going to remake them often or at all then they are the one to pick since they are faster.
var values : float[];
function Start () {
// Since we can't resize builtin arrays
// we have to recreate the array to resize it
values = new float[10];
// assign the second element(they start at 0)
values[1] = 5.0;
}
The second is a class. These are dynamic, meaning that they can be re sized at any time without remaking them. If you are going to resized them a TON this is the way to go since while they are much slower, you don't have to remake them(slower).
var vectors = new Array();
function Start(){
vectors.Push(Vector3(0,0,0)); /// Add a entry, it will resize it to one and assign it.
/// Manual or backend way(What is happening in the above line)
vectors.length(2); ///Size up by one
vectors[1] = Vector3(0,1,0); /// Assign second slot
}
The Array class is actually not UnityScript exclusive ;) It's just the .NET ArrayList class.
Always keep in $$anonymous$$d that both languages are .NET / $$anonymous$$ono languages. In general both (or all 3) can do the same things. However there are some language specific features which is unique for each language.
UnityScript isn't a standardized language. It's actually built on top of the boo compiler. So in general C# usually has more features since it was developed along with .NET, however .NET can do more than C# can so single languages can implement features which are possible in the framework, but not in other languages.
edit
I forgot to mention that the Array class should be avoided in all languages since it's dynamically typed. Use a generic list ins$$anonymous$$d. All languages support it.
If you still have trouble to get it working in UnityScript, just search and find things like that
Thanks for fixing my mistake, i also agree that using the Array class is bad but my post doesn't seem to evaluate this, editing it now.
Thanks. :) Put "$$anonymous$$SDN [insert term here]" into Google and you should get appropriate links, for example Queue.
Your answer
Follow this Question
Related Questions
Filling an array with child vectors 2 Answers
Avoiding instantiating two things in same location? 1 Answer
Comparing vector 3 locations 1 Answer
Javascript - find a value in an array 0 Answers