- Home /
Push a Vector3 array
I'm not quite sure what i've defined here,
var vectorarray = [Vector3(0,0,0)];
i would think that it would be the same as
var vectarray =new Array();
but evidently it is not, for, i have a script that i got off the internet wouldn't work if i used the latter (latter means second, to who were just confused)
I want to be able to push a value to that array, but if I use the first method i get the following error:
MissingMethodException: UnityEngine.Vector3[].Push
I usually define arrays the second way, so I'm not too sure what i have did in the first method, or why it isn't working
My actual goal is to put a value to the end of my vectarray
do you know of another method using the first way of defining arrays??
i went as far as to try this
vectarray[vectarray.length] = Vector3(0,0,0);
//sadly even this didn't work.
thanks.
.
.
.
.
.
.
.
Ps. This is the script i copied, it is part of playerprefsx
// i don't know what much of it means, that's why i put this at
//the bottom as to not scare anybody, i don't think you need to
// know it to solve my problem.
static function SetVector3Array (key : String, vector3Array : Vector3[]) : boolean {
return SetValue (key, vector3Array, ArrayType.Vector3, 3, ConvertFromVector3);
}
//and that ConvertFromVector3 function is:
private static function ConvertFromVector3 (array : Vector3[], bytes : byte[], i : int) {
ConvertFloatToBytes (array[i].x, bytes);
ConvertFloatToBytes (array[i].y, bytes);
ConvertFloatToBytes (array[i].z, bytes);
}
//and ConvertFloatToBites is
private static function ConvertFloatToBytes (f : float, bytes : byte[]) {
byteBlock = System.BitConverter.GetBytes (f);
ConvertTo4Bytes (bytes);
}
Answer by rutter · Mar 17, 2014 at 09:42 PM
Unity wiki: Which Kind Of Array Or Collection Should I Use?
//built-in Vector3[] array
var vectorarray = [Vector3(0,0,0)];
//JavaScript Array()
var vectarray = new Array();
Personally, I'm a big fan of static typing (especially considering that JavaScript's dynamic typing isn't well-supported on mobile), which means that I scratch and hiss at any code that uses Array()
. Its performance is less than ideal, and I have no sure idea of what's in it.
Built-in arrays have a fixed size once they're created, which is why there's no Push()
function for them. These are much closer to the traditional arrays you'll see in languages like C++. They have excellent performance if you know the size of the array ahead of time.
If you're fine using Array()
, that's okay. Really not my thing, but plenty of people use it.
One step up, in my opinion, is to use a generic list, or List
. This still guarantees that everything in the list has a particular type, but also allows you to add and remove items, resize the list, and so on. In many cases, that's the best of both options.
Your answer
Follow this Question
Related Questions
Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer
What is the order/connection of array elements in mesh.vertices? 2 Answers
c# : Accessing Struct variables inside an Array 1 Answer
Read variable from txt to Vector3 1 Answer
Vector3 Position returns 0 when applied to GameObject 1 Answer