- Home /
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;
}
}
Next time please highlight your code and press the "10101" button. It will give you a nice and readable code.
Use a List ins$$anonymous$$d 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.
Answer by Mike 3 · Apr 18, 2011 at 02:57 PM
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
Thanks i found it. var ObjectList : List.; and i had to replace ObjectList.sort to ObjectList.Sort.
Thanks a bunch!
Your answer

Follow this Question
Related Questions
Custom sorting function - need help 2 Answers
Can I use my own input device? 4 Answers
How to call a custom object from a C# dll 1 Answer
Editor Window with settings and Asset Creation 1 Answer
Creating an asset from script? 1 Answer