- Home /
Populating a Vector2 array in Javascript
For some reason, I can't seem to populate a vector2 array in Javascript.
function verts2uv(p:Vector3[]){ var arr:Vector2[]; for(var q:int=0;q<p.length;q++){ arr[q]=Vector2(p[q].x,p[q].y); } return arr; }
I keep on getting this error: NullReferenceException: Object reference not set to an instance of an object
Answer by Jessy · Jan 01, 2011 at 05:12 AM
First, your code looks TERRIBLE. Use the space bar! Start your function names with a capital letter. And there is already an implicit conversion from Vector3 to Vector2.
The problem is that arr has not been initialized. You need to create and array of the appropriate size, before you do anything with it.
function Verts2UV (verts : Vector3[]) {
var uv = new Vector2[verts.length];
for (var i = 0; i < uv.Length; i++) uv[i] = verts[i];
return uv;
}
oh also... i'm usually on a tiny screen, so spaces are spacy xD ... i'm reserving cap's for functions more likely to be public, than private intermediaries like these
No. That's what a Generic List is for. Available in JavaScript as of Unity 3.0.
Buy a bigger screen. If your code has to look like that, there are better things to do with your life than coding.
neat to find that vec3 to vec2 doesn't need to be re-componentized!