• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

New Update Text Issue 0 Answers

Is there any overhead when using Update functions for making object references? 1 Answer

Play method only ONCE when detected by the update function 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges