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
0
Question by Essential · Nov 26, 2012 at 05:22 PM · errorswitchenum

Enum switch statement error

 enum GameType { Survival, Escape, Prevail }
 
 var game = GameType.Survival;

In custom Editor script:

 switch (target.game)
 {
     case GameType.Survival:
         Debug.Log("hello");
 }

Error: Operator '==' cannot be used with a left hand side of type 'System.Object' and a right hand side of type 'GameType'.

Comment
Add comment · Show 4
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 Eric5h5 · Nov 26, 2012 at 05:39 PM 0
Share

How are you defining "target"?

avatar image Essential · Nov 26, 2012 at 05:40 PM 0
Share

this is at the top of the script:

 @CustomEditor (LevelData)
 
 class LevelData_CustomInspector extends Editor
 {
avatar image Eric5h5 · Nov 26, 2012 at 06:01 PM 0
Share

No, how are you defining the "target" variable?

avatar image Essential · Nov 26, 2012 at 06:05 PM 0
Share

It isn't defined. I've been using target.[member] in many other ways just fine up until the switch function. I think it's possibly a bug just localized to the switch function.

Anyways, I posted an answer that got around it.

2 Replies

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

Answer by Essential · Nov 26, 2012 at 06:00 PM

Okay I figured it out… For some reason the switch function doesn't properly understand the type when referencing from another script (?). So it needs to be specifically cast before it can be used. Very strange. Anyway… this fixed it:

 var currentGameType : GameType = currentArea.game;
 switch (currentGameType)
 {
     case GameType.Survival:
         Debug.Log("hello");
 }
Comment
Add comment · Show 2 · 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 Eric5h5 · Nov 26, 2012 at 06:07 PM 0
Share

This isn't actually the correct answer; you have something else going on. For example, this works fine...if you have this script, called Example.js:

 enum GameType { Survival, Escape, Prevail }
 
 var game = GameType.Survival;

And this script, called Example2.js:

 var target : Example;
 
 function Start () {
     switch (target.game) {
         case GameType.Survival:
             Debug.Log("hello");
             break;
     }
 }

It works with no issues.

This is why I was asking how you are defining "target". Because it's apparently an Object, which won't work.

avatar image Essential · Nov 26, 2012 at 06:13 PM 0
Share

Hmm… I might have it wrong but I thought pointing at another script used "target" as a reference to that gameObject's script.

I haven't created it manually, and so I wouldn't know how to define it as anything else.

avatar image
0

Answer by KiraSensei · Nov 26, 2012 at 05:36 PM

Try to declare your var game as a GameType (still an enum, but the compiler knows that it is actually an int).

 var game:GameType = GameType.Survival;
Comment
Add comment · Show 2 · 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 Essential · Nov 26, 2012 at 05:43 PM 0
Share

Thanks but it's already defined as GameType, I just missed that out from my example code above. :)

avatar image Eric5h5 · Nov 26, 2012 at 06:00 PM 0
Share

Doing

 var game:GameType = GameType.Survival;

is 100% identical to

 var game = GameType.Survival;

Since the compiler sees that you are assigning a GameType to the variable, it's statically typed as GameType whether you specify the type or not.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BCE0051 Error 1 Answer

Enum is thought to be a float? 2 Answers

error BCE0022: Cannot convert error, please help 3 Answers

Switch case using enum javascript 2 Answers

hashtable: snapshot out of sync 2 Answers


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