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
2
Question by PixelMuncher · Jan 24, 2011 at 12:50 AM · javascriptweaponswitchenum

Cycling through enum with key press

I'm trying to create a weapon script for the player. I decided dump the different weapons/objects into and enum rather than using a SwitchWeapon function. However, now I'm kind of stuck. I would like to add the feature of using either the mouse scroll or the "q" and "e" keys to cycle between the weapons. I know exactly how I would do it had I created a weapon function, however I'm kind of at a loss for how to do this with an enum and I really don't feel like starting the script over using a weapon function. I suppose I could use a series of if else statements to check which WeaponType from the list is selected, but it seems like there should be a more intuitive way to do this.

Also, I'm not terribly familiar with using enum (obviously). Does the enum have integers corresponding to each element in the list? because then I suppose I could just increase/decrease that integer by one.

Here's some of the code I have:

The enum:

enum WeaponType {primaryFire, concreteBarrier, stationaryTurret, playerShield}; var weaponType : WeaponType;

//this is sent to a switch(weaponType) case if(Input.GetKeyDown("f")){ weaponType = WeaponType.primaryFire; } if(Input.GetKeyDown("1")){ weaponType = WeaponType.concreteBarrier; }
if(Input.GetKeyDown("2")){ weaponType = WeaponType.stationaryTurret; } if(Input.GetKeyDown("3")){ weaponType = WeaponType.playerShield; } if(Input.GetKeyDown("e")){ //what code needs to go here to switch to the next weapon/object in the enum list? } if(Input.GetKeyDown("q")){ //what code needs to go here to switch to the previous weapon/object in the enum list? }

Sorry this is so long. Thanks for any help in advance.

Comment
Add comment · Show 1
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 Proclyon · Jan 24, 2011 at 08:16 AM 0
Share

How come a "weapontype" is defined as "primaryFire" isnt'a weapon type supposed to be $$anonymous$$elee/Anti-Tank/Ranged/Energy-Based/Ballistic something like that ? Sounds confusing to say primary fire is a weapon type, I would call that a weapon firing mode ins$$anonymous$$d tbh

1 Reply

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

Answer by Eric5h5 · Jan 24, 2011 at 01:59 AM

Enums are essentially ints with fancy names, so to some extent you can treat them like ints. First you would need

private var numberOfWeaponTypes = System.Enum.GetValues(WeaponType).Length;

Then you can use

else if(Input.GetKeyDown(KeyCode.E)){
    weaponType += 1;
    if (weaponType == numberOfWeaponTypes) weaponType = 0;
}
else if(Input.GetKeyDown(KeyCode.Q)){
    weaponType -= 1;
    if (weaponType == -1) weaponType = numberOfWeaponTypes-1;
}

(It's a bit more efficient to use "else if" for them all aside from the first, since it skips over the ones that can't be used instead of evaluating them all. Also, using KeyCodes instead of strings is faster and gives you potential errors at compile-time instead of runtime.)

Comment
Add comment · Show 6 · 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 PixelMuncher · Jan 24, 2011 at 02:20 AM 0
Share

Thank you! This is perfect. Works like a charm. :) Yeah, it was that numberOfWeaponTypes variable I just couldn't figure out. Also, thanks for the tips. I always wondered if using $$anonymous$$eyCodes has any particular advantage. I'll be changing all my strings to $$anonymous$$eyCodes. As for the if statements, that was just lazy program$$anonymous$$g on my part. I was going to go back and change it eventually. :) Thanks again!

avatar image Eric5h5 · Jan 24, 2011 at 03:40 AM 0
Share

I should also mention that you can assign specific values in enums...for example, you could do "`enum WeaponType {primaryFire = 3, concreteBarrier = 8, stationaryTurret = 12, playerShield = 20};`", but that wouldn't be a good idea in this particular instance obviously.

avatar image Proclyon · Jan 24, 2011 at 08:15 AM 0
Share

hey I knew that but didn't even think of using enum like that! Thanks man :D You helped me out big time here just for answering somebody else's question

avatar image AnniJ · Jan 25, 2016 at 01:57 PM 0
Share

Hi,

I know this is an old topic, but does anyone know how to write this in c#?

Thanks!

avatar image TheNewNerd AnniJ · Aug 03, 2017 at 07:15 AM 0
Share

I couldn't figure out the bottom part but the first line I believe is written like

 private int numberOfWeaponTypes = System.Enum.GetValues(typeof(WeaponType)).Length;

avatar image Rebaken-Enterprises TheNewNerd · Feb 28, 2018 at 06:04 PM 1
Share
 int numWeapons = System.Enum.GetValues(typeof(WeaponType).Length;
 
 // Cast weapon to int to do math on it
 int wt = (int)weaponType;
 wt++;
 if( wt == numWeapons) wt = 0;
 weaponType = (WeaponType) wt;

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

Operator '==' cannot be used! 2 Answers

Switch case using enum javascript 2 Answers

Weapon pick up and switching script 2 Answers

Why doesn't this weapon switching script work 1 Answer

best way to weapon swapping 0 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