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 zye · Feb 15, 2013 at 02:41 PM · javascriptswitchenumcase

Switch case using enum javascript

Hi everyone.

I'm trying to do a switch case using enum in javascript. I get this error when I save the code: "Assets/Scripts/BoxManagerJS.js(186,32): BCE0019: 'spell01' is not a member of 'Object'. ". The error is linked to the switch case.

Here's my code:

 enum spellName {spell01, spell02, spell03, spell04};
 
 
 function Update()
 {
         if(Input.GetKeyDown(KeyCode.Keypad6))
     {
     checkInASpell(spellName.spell01);
     }
 
         if(Input.GetKeyDown(KeyCode.Keypad8))
     {
     checkInASpell(spellName.spell02);
     }
 
          if(Input.GetKeyDown(KeyCode.Keypad4))
     {
     checkInASpell(spellName.spell03);
     }
 
          if(Input.GetKeyDown(KeyCode.Keypad2))
     {
     checkInASpell(spellName.spell04);
     }
 }
 
 function checkInASpell (spellName)
 {
 
         switch(spellName)
     {
     case spellName.spell01:
         Debug.Log ("spell01");
         break;
 
         case spellName.spell02:
         Debug.Log ("spell02");
         break;
 
         case spellName.spell03:
         Debug.Log ("spell03");
         break;
 
         case spellName.spell04:
         Debug.Log ("spell04");
         break;
         }
 }

I want to call the function checkInSpell with a diffrent arg in relation to the keypad I pressed. For example, If I press the keypad6, I want to call checkInSpell using the arg spellName.spell01 to enter the case spellName.spell01

Thanks for your help :)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Feb 15, 2013 at 02:45 PM

The problem is one of scope.

Your enum is named spellName, but you're also passing in a parameter with the name spellName. The compiler will use the most local scope it can when there are duplicate names. In this case, your case statement is using the parameter, and looking for class properties of spell01, spell02, and so on, which don't exist on the parameter. This is what the error is complaining about.

You need to either rename your enum, or rename your parameter, or you can possibly include the script name when when you want to refer to the enum...

 YourScriptName.spellName.spell01

But I don't know if that works in Javascript. Your best bet would be to rename one of them. I personally always use upper case the initial character for my enum names, so SpellName.

Comment
Add comment · 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
0

Answer by zye · Feb 15, 2013 at 03:34 PM

Thanks a lot for your explication, now it's working perfectly !

For next readers, here's the code with the modification thanks to Dave Carlile. This code works perfectly.

     enum SpellName {spell01, spell02, spell03, spell04};
      
      
     function Update()
     {
     if(Input.GetKeyDown(KeyCode.Keypad6))
     {
     checkInASpell(SpellName.spell01);
     }
      
     if(Input.GetKeyDown(KeyCode.Keypad8))
     {
     checkInASpell(SpellName.spell02);
     }
      
     if(Input.GetKeyDown(KeyCode.Keypad4))
     {
     checkInASpell(SpellName.spell03);
     }
      
     if(Input.GetKeyDown(KeyCode.Keypad2))
     {
     checkInASpell(SpellName.spell04);
     }
     }
      
     function checkInASpell (spellName : SpellName)
     {
      
     switch(spellName)
     {
     case spellName.spell01:
     Debug.Log ("spell01");
     break;
      
     case spellName.spell02:
     Debug.Log ("spell02");
     break;
      
     case spellName.spell03:
     Debug.Log ("spell03");
     break;
      
     case spellName.spell04:
     Debug.Log ("spell04");
     break;
     }
     }

Thanks Dave Carlile ! :)

Comment
Add comment · 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

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

10 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

Related Questions

Operator '==' cannot be used! 2 Answers

js switch case warning 1 Answer

How to check if an enum’s case has changed 3 Answers

What is a more efficient way to write this Switch Statement? 3 Answers

Cycling through enum with key press 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