Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Mahunreah · Feb 09, 2021 at 02:19 PM · liststoggle button

I have list with 16 Toggles in it. Player should be able to always have X active. If he clicks X+1, the oldest toggle should switch off. What did I miss?

Hi :)

I have a List with 16 Toggles. The Player should be able to have X active at any given moment - let's say X=2.

I want the program to automatically de-activate the oldest entry of "toggled on"-Toggles in the List and set the clicked Toggle to active once the player tries to enabled a third toggle (or X+1).

This kind of works. I can ativate up to two Toggles and when I click on a different one, the oldest gets set to isOn=false.

BUT: This does not work if I click on a preveriously checked toggle which had been set to false by code. If I want to re-activate, I need to manuall uncheck an active Toggle and click on a previously checked toggle. I don't know what causes this behaviour.

This is the function which is called when a toggle gets clicked:

 public class CraftingRecipeUI 
 {
     [SerializeField] CraftingWindow craftingWindow;
 [...]
 
 public void ToggleChanged(bool toggle)
     {
         
         //if (craftingWindow.mitarbeiterTemp <= betrieb.MitarbeiterAnzahl)
         //{
             if (toggle == true)
             {
                 craftingWindow.mitarbeiterTemp++;
                 Debug.Log("Mitarbeiter Temp: " + craftingWindow.mitarbeiterTemp);
             }
             if (toggle == false)
             {
                 craftingWindow.mitarbeiterTemp--;
                 Debug.Log("Mitarbeiter Temp: " + craftingWindow.mitarbeiterTemp);
                 if (craftingWindow.mitarbeiterTemp < 0)
                 {
                     craftingWindow.mitarbeiterTemp = 0;
                 }
             }
         craftingWindow.Check();
         //}
     }
 }


And here's the Check()-Function:

 public class CraftingWindow : MonoBehaviour
 {
     public List<Toggle> MitarbeiterArbeitsplatz;
     public int mitarbeiterTemp;
     public Betrieb betrieb;
 
 [...]
 
 public void Check()
     {
         if (mitarbeiterTemp > betrieb.MitarbeiterAnzahl)
             {
                 Toggle firstArbeitskraft = MitarbeiterArbeitsplatz.Find(x => x.isOn == true);
                 //firstArbeitskraft.GetComponent<Button>().onClick.Invoke(); (I tried if maybe something would change if I made the code click the button, but it changed nothing)
                 firstArbeitskraft.isOn = false;
             }
     }
 [...]
 }


I'd appreciate your help. I think I might miss a check for some value somwhere, but I'm not sure about where to go from here.
Here's a video I made of how it looks like: link to a youtube video I uploaded

Comment
Add comment · Show 2
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 logicandchaos · Feb 09, 2021 at 02:51 PM 0
Share

There are toggle groups that do this for you.

avatar image Mahunreah logicandchaos · Feb 09, 2021 at 04:06 PM 0
Share

But when I use the toggle group element, only one thing can be active. That's why I needed to write code. Or do I misunderstand you?

1 Reply

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

Answer by Hellium · Feb 09, 2021 at 04:23 PM

The following is a rework of your code. You have some mixed up code IMHO. The responsibility to manage your toggles is split into two classes.

FOLLOWING CODE NOT TESTED

 public class CraftingRecipeUI 
 {
     [SerializeField] private Toggle[] toggles;
     [SerializeField, Min(1)] private int maxEnabledTogglesCount = 2;
     private List<Toggle> enabledToggles = new List<Toggle>();
     
     private void Start()
     {
         for(int toggleIndex = 0 ; toggleIndex < toggles.Length ; ++toggleIndex)
         {
             Toggle toggle = toggles[toggleIndex];
             toggle.onValueChanged.AddListener(value => ToggleValueChanged(toggle, value));
         }
     }
     
     // Remove any call to this method from the toggles in the inspector
     private void ToggleChanged(Toggle toggle, bool value)
     {
         if(value)
         {
             enabledToggles.Add(toggle);
             if(enabledToggles.Count > maxEnabledTogglesCount)
             {
                 enabledToggles[0].isOn = false; // This should remove the toggle from the list automatically
              }
         }
         else
         {
             enabledToggles.Remove(toggle);
         }
     }
 }

And taht's it. You won't need the code from the CraftingWindow class you've provided.

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 Mahunreah · Feb 09, 2021 at 08:40 PM 0
Share

Thank you so much! Looks like I was absolutely on the wrong path with my idea. This worked just like what I tried to accomplish and looks so much cleaner, too!

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

111 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

Related Questions

List Transform returns null 2 Answers

Modifying Lists via the inspector 1 Answer

Toggle button don't dissapear.. 1 Answer

Toggles/Toggle Groups Question 0 Answers

How do i detect if toggle has been unchecked 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