- Home /
error for array functions
ive been working on my pahing script and it works up to line 23 and 26 i get this eror mesige for both those lines.
Assets/controler.js(22,31): BCE0019: 'Push' is not a member of 'score[]'.
public var curwp : Transform; public var open : score[]; public var close ; var bestrate : float = 10000; function Start () { open = GetComponentsInChildren.<score>(); }
function Update () { bestrate = 10000; for (var val : score in open){ if (val.tlscr < bestrate){ bestrate = val.tlscr; print (bestrate); } } num = 0; for (var val : score in open){
if (val.tlscr == bestrate){
val.transform.renderer.material.color = Color.magenta;
print (val);
close.Add (val);
print (close);
print ("close = " + close);
open.RemoveAt(num);
}
num += 1;
}
}
any hints to what it could be???
Answer by almo · May 10, 2011 at 07:26 PM
If you want to use Push and stuff on arrays, you need to declare your variables a certain way. When you use the
public var myArray : float[];
construct, it's a C# array which does not have Push or RemoveAt.
Here's an example that pushes a float onto one array, and an object onto another.
public var testobjarr : Array; public var testfloatarr : Array;
function Start() { testfloatarr = new Array(); testobjarr = new Array(); }
function Update () { if(testfloatarr.length < 3) { testfloatarr.Push(1); } if(testobjarr.length < 1) { testobjarr.Push(this); } }
so do i need to youse function start and declare like that ??
Glad to help. The C# arrays Jessy is referring to are faster and more efficient, so you might want to learn to use those some time.
It's not a "C# array" though. Typically they are called built-in arrays, or .NET arrays. Also, the JS Array class is slow and obsolete; use List ins$$anonymous$$d.
Answer by Jessy · May 10, 2011 at 06:59 PM
Push, Add, and RemoveAt, are in fact, not members of Array.
You probably want to make open a List.<score>
, instead.
import System.Collections.Generic;
var moreDescriptiveNameThanOpen = List.<Score>(GetComponentsInChildren.<Score>());
(Score is a class, and therefore should be in PascalCase, not camelCase.)
list add that makes sence to me but what do you mean by making a list open
No, I said make open a List. This is also a good demonstration of how bad variable names can be confusing. ;-)
o sory my bad how do you make it a list is ther an eqivelent of [] or somthing
What about: http://unity3d.com/support/documentation/ScriptReference/Array.html According to that, Push, Add, and RemoveAt are members of Array. Jessy's link is to C# arrays.
thats true it dose say that push and add are members of array acording to the scripting refrences