Hi, i have been going crazy with this. In the Star function when i try to reference the member name of my custom class, unity says name is not a member of 'Object'. Now i dont know when my custom object changed to Object type. Even when i debug the object, it says it is of type info. Only when i reference any member variable, it gives this error. Any help will be appreciated.
#pragma strict
private var ObjectList :Array = new Array();
function AddObject(tname : String , tvalue : int){
var newinfo : Info = new Info();
newinfo.name = tname;
newinfo.value = tvalue;
ObjectList.Add(newinfo);
}
function SortObjects(thisObject : Info,thatObject : Info) {
if (thisObject.value < thatObject.value)
{
return 1;
}
else if (thisObject.value > thatObject.value)
{
return -1;
}
return 0;
}
function Start(){
AddObject("demo1",33);
AddObject("demo2",66);
AddObject("demo3",22);
ObjectList.sort(SortObjects);
Debug.Log(ObjectList[0]);
Debug.Log(ObjectList[0].name);
var temp : Info = new Info();
temp = ObjectList[0];
Debug.Log(temp.getName());
}
class Info {
var name : String;
var value : int;
function getName(){
return this.name;
}
}
arrange it properly
– anon16754120Next time please highlight your code and press the "10101" button. It will give you a nice and readable code.
– FLASHDENMARKUse a List instead of an Array; Array is obsolete.
– JessyBut how do i use List in this case? I am kind of new to jscript and unity. But i added import System.Collections.Generic; and initialised a List but it says unknown keyword List. Can you explain how will i do that? thanks.
– anon28235660