- Home /
Comparing enums and ints (javascript)
Why does this work:
var _mode : int;
_mode = manager.gameMode;
if (_mode == mode.editSetup) {
//do some stuff
}
But not this:
if (manager.gameMode == mode.editSetup) {
//do some stuff
}
The compiler insists that manager.gameMode is an Object, even though it is declared this way (in another script):
var gameMode : int = 0;
mode is an enum, and the compiler is clearly treating it as int in this case (it complains that I'm trying to compare an Object to an int). The manager script has other vars in it that I reference without any problem. Why would the compiler think that this particular var reference is an object and not an int?
In both examples, manager is declared like this:
manager = gameObject.GetComponent("manager_script");
Both of these examples are from the same function (in a script attached to the same GameObject as manager_script). I just rewrote what you see in example 2 b/c it wouldn't compile.
I would say that's your problem.
The documentation specifically says that GetComponent returns a Component. Until you cast it to something else, that's what the compiler will think it is.
The code I just posted in my answer demonstrates that what you're trying to do should work (as long as the compiler knows what kind of object your manager is).
Answer by jahroy · Nov 30, 2011 at 10:20 PM
The following script works for me:
/* define a test enum */
enum TestEnum { Funky, Nifty, Super }
/* define a public var of type TestClass */
public var theTestObject : TestClass;
/* define TestClass */
class TestClass
{
public var theMember : TestEnum;
}
/* test it */
function OnGUI ()
{
var theLabel : String;
if ( theTestObject.theMember == TestEnum.Funky ) {
theLabel = "Quite Funky";
}
else {
theLabel = "Not so Funky";
}
var theRect : Rect = Rect(200, 200, 200, 50);
GUI.Label(theRect, theLabel);
}
Isn't that the same thing that isn't working for you in your second example?
So you're defining a separate class within this javascript file, which is distinct from the class implicitly defined by the filename? I should probably understand why you're doing that, since your example works and $$anonymous$$e doesn't. Again the manager script has lots of variables of different kinds that I am able to reference successfully with manager.variableName, but this is the only case where the compiler interprets manager.variableName as an Object. And I have tried to define that game$$anonymous$$ode variable in the manager script as var game$$anonymous$$ode : mode, but I get the same error. Thanks for all you help!
The word "mode" is an awful questionable word to choose for the name of your enum.
I would give it a more unique name, just in case somebody else in the $$anonymous$$ono universe has already decided to use "mode" for something else.
I define TestClass in the same file because (like you say) it is a simple class.
Here is my understanding (it could be wrong):
When using javascript in Unity, if you create a class inside a file of the same name, you are implicitly declaring your class to be a $$anonymous$$onoBehaviour.
This means that it can be added to GameObjects (as a Component) and its Start(), Awake(), Update() and OnGUI() functions will be recognized by Unity (among other things).
For each project I work on, I create dozens of small helper classes like TestClass and put them in a file named ProjectNameClasses.js.
I honestly have no idea how a project could exist without using classes like this.
Yes, if I had a separate script named TestClass, I would have to attach that script to a GameObject.
That's how $$anonymous$$onoBehaviours work.
In that case, I would have to use GetComponent.
The important thing is that I would not put quotes around the word TestClass if I use GetComponent().
An alternative to using GetComponent would be to declare your variable at the top of the script and drag and drop a GameObject with a script of that type attached to it onto the appropriate slot in the Inspector.
In other words, I would put this at the top of the script:
var someTestObject : TestClass;
I could then select an object with this script and look in the inspector to see a slot named Some Test Object.
You can drag any object that has a TestClass script attached to it onto this slot. After doing this you can reference the variable named someTestObject in your script along with all the variables and functions associated with the TestClass script.
Here's a good rule of thumb for program$$anonymous$$g:
Anything done with Strings is likely to be questionable. They're not very efficient and they open the door for lots of human error.
In the case of the GetComponent function, they can apparently cause ambiguity when you ask for a component by name.
Enums are a great way to avoid using them, so you're already headed in the right direction by using Enums like you are.
Regarding your question/answer... You don't have to do anything. You asked a question and there is only one answer ($$anonymous$$e) so far.
The rest of our discussion has been done through comments, which is the preferred approach.
If all that junk I wrote answered your question, you have the option to click the green check mark next to my answer. This indicates to others that your issue is resolved and that my answer was helpful.
Your answer

Follow this Question
Related Questions
comparing enum from a class in an editor script doesn't work 2 Answers
Can't compare byte to byte? 2 Answers
Game Engine Comparison 0 Answers