Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by icemacsea · Nov 30, 2011 at 08:50 PM · enumcomparison

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Nov 30, 2011 at 10:02 PM 0
Share

How does manager get declared in the second example?

avatar image icemacsea · Nov 30, 2011 at 10:12 PM 0
Share

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.

avatar image jahroy · Nov 30, 2011 at 10:24 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image icemacsea · Nov 30, 2011 at 10:41 PM 0
Share

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!

avatar image jahroy · Nov 30, 2011 at 11:23 PM 0
Share

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.

avatar image jahroy · Nov 30, 2011 at 11:37 PM 0
Share

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.

avatar image jahroy · Dec 01, 2011 at 12:28 AM 1
Share

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.

avatar image jahroy · Dec 01, 2011 at 01:14 AM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

comparing enum from a class in an editor script doesn't work 2 Answers

Can't compare byte to byte? 2 Answers

In Unity how can I set a value to property while initializing a prefab with Instantiate() method like OOP constructor with parameters? 1 Answer

Game Engine Comparison 0 Answers

Compare look rotations, but only for one axis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges