Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 01, 2016 at 10:07 AM by winzel123 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by winzel123 · May 01, 2016 at 07:15 AM · listfor-loopkeycode

How to make a for loop statement function like a switch statement?

I have a lists of Ability class which all my abilities are inherited from. Ex. Fireball and Frostbolt

 public Lists<Ability> abilitiesList = new List<Ability>();

In the update method I made a for loop, if a specific KeyCode from the list is clicked the bool SkillIsActivated turns true.

 void Update()
 {
     for (int i = 0; i < abilitiesList.Count; i++)
     {
             if(Input.GetKeyDown(abilitiesList[i].SkillActivationCode))
             {
                     abilitiesList[i].SkillIsActivated = true;
                     //If not iterated in the loop make the bool false. 
                     //or deactivate others on the list.
             }
     }
 }

This code works but my problem is that how can I make the SkillIsActivated turn to false if that specific KeyCode is not clicked or used? Just like a switch statement logic.

I'm not really sure if using a for loop on this problem works.

I'm stuck with this problem for 5 days now maybe I think its time for me to ask the community about this.

I really hope someone can help me :(

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 gjf · Apr 30, 2016 at 11:49 PM 0
Share
 else
 {
     abilitiesList[i].SkillIsActivated = false;
 }
avatar image winzel123 gjf · May 01, 2016 at 08:42 AM 0
Share

Tried it, doesn't work

avatar image Bunny83 winzel123 · May 01, 2016 at 09:06 AM 1
Share

Of course it does work, but you might expect something different. $$anonymous$$eep in $$anonymous$$d that "Get$$anonymous$$eyDown" is only true for one single frame. So the moment you press down the right key, you will set "SkillIsActivated" to true. But the next frame when the loop is run again "Get$$anonymous$$eyDown" will return false as the key wasn't pressed this frame. Therefore the second frame will set all SkillIsActivated variables to false.

At least that's the expected behaviour. If you want a different behaviour, tell us what you want. If you want only to be able to "activate" a new skill when all skills are deactivated, you have to check that manually.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

RemoveAt() not working within for() loop. 1 Answer

How to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer

Sorting a list into multiple smaller lists 1 Answer

For loop does not loop C# 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