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 /
  • Help Room /
avatar image
0
Question by mvpires · Mar 11, 2016 at 01:46 PM · scripting problemgameplayloopingweaponsswitching

Switching items - can't detect a crazy loop that is happening.

So, I'm trying to switch weapons in my game. The player has 3 different ones. Each weapon has an ID that I use to detect wich weapon is active and I call the method to switch weapons when the player presses the "X" key.

 if (Input.GetKey(KeyCode.X))
             {
                 ChangeWeapons();
             }


This is the method (please, ignore the horrible hard coded parts haha.)

 void ChangeWeapons()
     {
 
         if (mCurrentWeapon == 1 | mCurrentWeapon == 0)
         {
             mCurrentWeapon++;
 
         }
         else if (mCurrentWeapon == 2)
         {
             mCurrentWeapon--;
         }
 
         switch (mCurrentWeapon)
         {
             case 0:
                 mWeapons[0].GetComponent<MeshRenderer>().enabled = true;
                 mWeapons[1].GetComponent<MeshRenderer>().enabled = false;
                 mWeapons[2].GetComponent<MeshRenderer>().enabled = false;
                 break;
             case 1:
                 mWeapons[1].GetComponent<MeshRenderer>().enabled = true;
                 mWeapons[0].GetComponent<MeshRenderer>().enabled = false;
                 mWeapons[2].GetComponent<MeshRenderer>().enabled = false;
                 break;
             case 2:
                 mWeapons[2].GetComponent<MeshRenderer>().enabled = true;
                 mWeapons[0].GetComponent<MeshRenderer>().enabled = false;
                 mWeapons[1].GetComponent<MeshRenderer>().enabled = false;
                 break;
         }
 
     }


What is happening is a crazy loop that changes the weapons three or four times at once. I need to fix this. Any suggestions? Thanks for the attention in advance.

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
1

Answer by phil_me_up · Mar 11, 2016 at 01:48 PM

You might want to look here: http://docs.unity3d.com/ScriptReference/Input.html

The 'GetKey()' method will return true whilst a key is held down, so unless you tap and release the key within the space of one frame (assuming you're checking in the Update loop), you will be calling Change Weapon a lot.

You probably want to switch to GetKeyUp or GetKeyDown

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 Lo0NuhtiK · Mar 11, 2016 at 02:04 PM 0
Share


@mvpires :

Just as an added note, you might want to do the rest something like this :

 $$anonymous$$eshRenderer[] weapons;
 int currentWeapon;
 
 void ChangeWeapon()
 {
      currentWeapon++;
 
      if(currentWeapon >= weapons.Length) currentWeapon = 0;
 
      for(int i = 0; i < weapons.Length; i++)
      {
           if(i == currentWeapon) weapons[i].enabled = true;
           else weapons[i].enabled = false;
      }
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Picked up items being added multiple times on the inventory list. 1 Answer

Scene Switching problem. Scene becomes black when it changes 2 Answers

Switch between 5 cameras in the game by clicking. 0 Answers

How I can count how many time gameOver scene is loaded ? 1 Answer

How to Switch GameObjects (Weapons) by reaching a specific amount of score? 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