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 Theacesofspades · Nov 11, 2015 at 05:37 AM · functionloopclassinventorystacking

Why does this part of code run twice?

I am making a basic inventory with stacking and for some reason whenever i click on the item it subtracts 2 instead of just 1. Can anyone help me? Thank you

 void OnGUI () 
     {
         GUI.DrawTexture(windowRect, inventoryBackgroundTexture);
 
         for (int x=0; x<across; x++)
         {
             for (int y=0; y<down; y++)
             {
                 Rect buttonRect = new Rect(_offset*2+buttonSize*x, _offset*2.5f+buttonSize*y, buttonSize, buttonSize);
 
                 if (inventory[x, y] != null)
                 {
                     if (inventory[x, y].itemCount > 0)
                     {
                         GUI.DrawTexture(buttonRect, inventory[x, y].texture);
 
                         Vector2 mousePos = Input.mousePosition;
                         mousePos.y = Screen.height-Input.mousePosition.y;
 
                         if (buttonRect.Contains(mousePos))
                         {
                             if (Input.GetButtonDown("Fire1"))
                             {
                                 SubtractItem(x, y); *** This part runs 2 times ***
                             }
 
                             GUI.DrawTexture(buttonRect, hoverTexture);
                         }
 
                         if (inventory[x, y] != null)
                         {
                             GUI.Label(buttonRect, inventory[x, y].itemCount.ToString(), itemCountStyle);
                         }
                     }
                 }else{
                     GUI.DrawTexture(buttonRect, blankTexture);
                 }
             }
         }
     }
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 NvGBoink · Nov 11, 2015 at 10:41 AM 0
Share

$$anonymous$$ight have the script on the game object twice :/ Also where is the SubtractItem(); ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aranda · Nov 11, 2015 at 01:23 PM

Probably you have two cameras with Gui Layer on them? You should move any input handling out into the Update() function.

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 Theacesofspades · Nov 11, 2015 at 04:44 PM 0
Share

There is only 1 game object with a single script on it. I have no clue why it runs twice. I'll try moving the input to the update function but I don't think that will help my problem.

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

34 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

Related Questions

Question about protected and abstract classes. 1 Answer

Void being called 4 times, no apparent reason 1 Answer

List check argument of item 3 Answers

[Help] I don't know how to create a stacking system for my inventory! 3 Answers

Nested coroutines 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