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 /
avatar image
0
Question by $$anonymous$$ · Oct 15, 2017 at 01:56 PM · updateswitchupdate functionupdate problemswitch-case

Switch statement not running on every frame

Hello there! This will probably be really obvious and simple but the switch statement I have implemented in my code does not exactly work 100%. All I'm trying to do is pick up a weapon, hold down a button to charge a throw then release the button to fire it (like throwing a spear). I have my methods for charging the throw and releasing (I think) but for now I'm just trying to get it to work with simple Debug.Log statements. As you can see down in my switch statement where it says "Debug.Log("CHARGING");" I want that to appear every frame as long as I hold down the button and as soon as I release said button it should print "FIRE" once.

As of right now it only seems to print out "CHARGING" once while I hold the button which isn't right at all. I know something probably isn't right with one of my statements but could anyone help debug my code and point out where I screwed up? This is my first post so any help is great, thanks!

Edit: All this code is running within the update function for a little extra info in case anyone thought this was why it wasn't working.

Code

     if (playerOnWeapon)
     {
         switch (pickedUpWeapon)
         {
             
             case false:
                 if (Input.GetKeyDown(KeyCode.M))
                 {
                     pickedUpWeapon = true;
                     weapon.transform.parent = weaponGrabber;
                     weapon.transform.position = weaponGrabber.transform.position;
                     weapon.transform.rotation = weaponGrabber.transform.rotation;
                     weaponRB.isKinematic = true;
                     Debug.Log("You picked up the weapon");
                 }
                 break;

             case true:

                 if (Input.GetKey(KeyCode.N)) //Holding down charge button
                 {
                     if (!isCharging)
                     {
                         isCharging = true;
                         Debug.Log("CHARGING");
                     }
                     
                 }

                 else //No longer holding down the charge button
                 {
                     if (isCharging)
                     {
                         pickedUpWeapon = false;
                         isCharging = false;
                         Debug.Log("FIRE");
                     }
                     
                 }
                 break;
         } 
     }
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

1 Reply

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

Answer by YoucefB · Oct 15, 2017 at 02:23 PM

You just need to remove if (!isCharging) statement.

    if (Input.GetKey(KeyCode.N)) //Holding down charge button
                      {
 // once you hold N isCharging will switch to true..
                        //  if (!isCharging) // meaning it will skip this part next time until its false again when you release N key.
                          //{
                              isCharging = true;
                              Debug.Log("CHARGING");
                        //  }
                          
                  }

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Oct 15, 2017 at 02:56 PM 0
Share

Thank you!

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

70 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to call PlayerPrefs in an Update function without hogging memory? 0 Answers

Help with sound playing when a certain bool = false 1 Answer

Disable an if statement in update 3 Answers

What is the download size of updating from version 2019.3 to 2019.4.1f1? 0 Answers

void update working under conditions 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