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 BrighterWorld · Feb 05, 2020 at 09:26 PM · menuui imageincrement

Increment function skips number 2.

I have this script which is supposed to increment or decrement the number when I press the right buttons. When I increment it for the first time it says 2 and then 3 but after that, when I decrement it, it says 1 and when I increment it it says 3, completely skipping the number 2. Both UI buttons have this script attached and are using the right functions. Images are correctly assigned. Also, even though the number is 3, it doesn't change the Image to img3. Here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class selector : MonoBehaviour
 {
      int counter = 1;
     public Image img1;
     public Image img2;
     public Image img3;
     void Start()
     {
         img1.enabled = true;
         img2.enabled = false;
         img3.enabled = false;
     }
 
       public void Incrementer() 
     {
         if (counter > 0 && counter < 3)
         { counter ++; }
         Debug.Log(counter);
     }
     public void Decrementer()
     {
         if (counter > 1 && counter < 4)
         { counter --; }
         Debug.Log(counter);
     }
     void Update()
     {
         if (counter == 1) { img1.enabled = true; img2.enabled = false; img3.enabled = false; }
         if (counter == 2) { img1.enabled = false; img2.enabled = true; img3.enabled = false; }
         if (counter == 3) { img1.enabled = false; img2.enabled = false; img3.enabled = true; }
     }
   
 }
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
Best Answer

Answer by phosphoer · Feb 05, 2020 at 11:22 PM

Let me see if I understand your setup correctly:

You have in your UI 2 buttons, one is supposed to increment a counter, the other is supposed to decrement. You are displaying a counter which shows numbers 1-3 (img1, img2, img3). You have the above script attached to both the increment and decrement buttons, and on each one you've linked their OnClick to either Incrementer() or Decrementer(). On both instances of the script you have hooked up the references to img1-3.


If my understanding is accurate, here is what your problem likely is:

By attaching the script to both buttons, you are running 2 instances of this script simultaneously. That means both buttons have their own 'counter' variable and are both running their Update() method independently of each-other. So when you click the Increment button, its counter goes up to 2, but the Decrement button's counter stays at 1. Since they are both referencing the same img1-3, they will 'fight' over which image is enabled. Depending on which one's Update() runs last, you'll see varying results on what number your counter image displays, hence the weird behavior you are describing.

To fix:
You should structure your game object hierarchy such that you have a parent object named 'Counter' or something, and attach your script to that. The Increment and Decrement buttons, and img1, img2, img3 will go under it. Then the OnClick events for each button should reference the same script on the 'Counter' object. In other words, there should be only 1 game object with your script attached to it, per counter that you want to display.
Hope this helps! If I'm assuming incorrectly about your setup i'll update my answer.

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 BrighterWorld · Feb 09, 2020 at 05:35 AM 0
Share

Thank you! This is exactly what I did and it's working flawlessly

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

122 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 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

[Partially Solved] OnClick of one button, show different set of buttons. 1 Answer

How to change Menu Buttons to a Selected Image and Deselected Image 2 Answers

How to Change Menu Button Rollover States with GameObjects 1 Answer

boolean flicker 3 Answers

How to make a Main Menu 3 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