Is not a memeber of Object

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

Next time please highlight your code and press the "10101" button. It will give you a nice and readable code.

Use a List instead of an Array; Array is obsolete.

But 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.

1 Answer

1

The problem is that Array isn't typed - everything that goes in there gets stored as a System.Object reference

What you could do instead is use a List.< Info> instead, which requires you to add import System.Collections.Generic; to the top of your script

For more info on that, http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Just remember to use a . before the < each time :D

Hi, can you explain how do i accomplish that?

Thanks i found it. var ObjectList : List.<Info>; and i had to replace ObjectList.sort to ObjectList.Sort. Thanks a bunch!