- Home /
comparing enum from a class in an editor script doesn't work
Hi,
Got a twisted problem, but hopefully solvable.
I am building a custom interface editor. In there, I want to display only the necessary fields, so I need to check values to know what to show and in which case. The problem is that the target value is an enum taken from a class in another script, and it doesn't let me do comparison with it.
in a script I have:
enum AxisList {xAxis,yAxis,zAxis}
class jf_pivotDrive { / I Use this class to hold the enum because I can't seem to be able to access the enum from the reference of the component attached to a RB / static var AxisList: AxisList;
}
in the script target ( linked to the editor)
public var whichAxis = jf_pivotDrive.AxisList.xAxis;
and finally in the editor script
if ( target.whichAxis == jf_pivotDrive.AxisList.xAxis)
I get the following error:
The name 'jf_pivotDrive.AxisList' does not denote a valid type. Did you mean '.AxisList' ?
What should I do to have this work? should I define the class hoding the enum somewhere else without breaking other script using it? Is namespace should be involved if possible?
I know I could get away with this by simply using a string all the way instead of enum, but I am here to learn, and enum if a powerfull, trouble free feature and convenient gui component. Any advice or tips welcome.
thanks,
Jean
Answer by Eric5h5 · Aug 20, 2010 at 07:41 AM
Enums are project-wide; don't try to refer to specific scripts. i.e., not jf_pivotDrive.AxisList, just AxisList. Although enums defined in editor scripts are not available in regular scripts (but vice versa is true...enums defined in regular scripts are available in editor scripts).
thanks Eric5h5, it works now. I had to overcome as well a casting problem, I explained it in my other answer.
Answer by Jean-Fabre · Aug 20, 2010 at 10:01 AM
ok, Eric5h5 is right (thanks again) and it got rid of the error I mentionned.
Tho, it did not solved all my issues, so for the completness of the example given I post here what worked for me, yet I think it can be improved but I haven't found how.
if you use
if ( target.whichAxis == AxisList.xAxis )
then it throws a new error
Operator '==' cannot be used with a left hand side of type 'System.Object' and a right hand side of type 'AxisList'.
and I could not cast it directly in the comparison line, so I used a temp variable for it and it works
var temp: AxisList = target.whichAxis;
if ( temp == AxisList.xAxis )
That works but I am sure it could be done in the same line for purist.
Jean
Your answer

Follow this Question
Related Questions
UIElements EnumField data binding 0 Answers
Enum Value Changing At Runtime On A Prefab 1 Answer
Dynamic serialized fields based on enum 0 Answers