Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Check Number of active Toggles

Discussion in 'Scripting' started by NavpointSoftware, Nov 22, 2018.

  1. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    Hi All

    I have this function which is a list of toggles which are cycled and randomly activated or not.

    I basically want to add a check that makes sure that a minimum of at least 3 toggles have been activated in the cycle.

    What is the optimal way of doing this or should I just use a second for each loop counting which toggles are active and then go back over the first loop?

    Kind Regards,

    John

    Code (CSharp):
    1.     //Random Checklist Button
    2.     public void SelectRandomChecktoggles()
    3.     {
    4.  
    5.         int tf;
    6.    
    7.         foreach (Toggle t in togglestartMenuArray)
    8.         {
    9.             tf = UnityEngine.Random.Range(0, 3);
    10.            
    11.             if (tf <= 1)
    12.             {
    13.              
    14.                 t.isOn = false;
    15.            
    16.  
    17.             }
    18.             else
    19.             {
    20.  
    21.                 t.isOn = true;
    22.  
    23.             }
    24.  
    25.         }
    26.  
    27.         sfxMenuButtonPress();
    28.  
    29.     }
     
  2. Code (CSharp):
    1. //Random Checklist Button
    2.     public void SelectRandomChecktoggles()
    3.     {
    4.         int tf, noOfActivated;
    5.  
    6.         foreach (Toggle t in togglestartMenuArray)
    7.         {
    8.             tf = UnityEngine.Random.Range(0, 3);
    9.          
    10.             if (tf <= 1)
    11.             {
    12.            
    13.                 t.isOn = false;
    14.          
    15.             }
    16.             else
    17.             {
    18.                 t.isOn = true;
    19.                 noOfActivated++;
    20.             }
    21.         }
    22.         if (noOfActivated > 2)
    23.         {
    24.              // at least 3 buttons have been activated
    25.              // do something
    26.         }
    27.         sfxMenuButtonPress();
    28.     }
     
  3. NavpointSoftware

    NavpointSoftware

    Joined:
    Apr 30, 2011
    Posts:
    102
    That was easier than i was expecting.

    Many thanks!
    John