• 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
Question by Hathakas · Sep 21, 2015 at 04:38 PM · arraystoggle

Help with Arrays - Check if only 1 is toggled

Hey guys,

I don't know if I'm going about t$$anonymous$$s the right way. Basically I need to be able to click a toggle, and when I click the toggle, I need to change the alpha of a few buttons. I wanted to do it in an Array because I'll be adding more buttons called Slots later

 public void ShowAllSlot()
     {
         
         GameObject[] buildOptionsFinder = GameObject.FindGameObjectsWithTag("BuildOptions");
 
         buildOptions = new Toggle[buildOptionsFinder.Length];
 
         for (int i = 0; i < buildOptionsFinder.Length; i++)
         {
             buildOptions[i] = buildOptionsFinder[i].GetComponent<Toggle>();
 
 
 
 
             //Debug.LogError(buildOptions[1]);
             if (buildOptions[i].isOn )
             {
                 GameObject[] slotFinder = new GameObject[transform.c$$anonymous$$ldCount];
                 
                 for (int ii = 0; ii < transform.c$$anonymous$$ldCount; ii++)
                 {
                     slotFinder[ii] = GameObject.Find("Level1Slots").transform.GetC$$anonymous$$ld(ii).gameObject;
 
                     Image button = slotFinder[ii].transform.GetC$$anonymous$$ld(0).transform.GetC$$anonymous$$ld(0).transform.GetComponent<Image>();
 
                     
 
                     Debug.LogError("IT SHOULD BE SHOWING NOW");
                     button.color = Alpha40;
                     
                 }
             }
 
   
 
 
         }
 
     }

I can't seem to figure a way to say :

if (1 out of all buildOptions[i].isOn == true) do t$$anonymous$$s else if (all buildOtions[i].isOn == false) do t$$anonymous$$s

Thanks!

Comment
Xephex
Mortalanimal

People who like this

2 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by flashframe · Sep 21, 2015 at 09:36 PM

You could use a bool and a foreach loop to check that at least one of the toggles is selected. Somet$$anonymous$$ng like t$$anonymous$$s (I haven't tested t$$anonymous$$s code sorry, just writing it from memory).

 bool isToggled = false; //declared at the start of your class
 
 foreach(Toggle t in buildOptions){
     if(t.isOn){
         isToggled = true;
     }
 }
 
 if(isToggled){
     //Do somet$$anonymous$$ng
 }else{
     //Do somet$$anonymous$$ng else
 }
 
 isToggled = false;
Comment
Hathakas
Mortalanimal

People who like this

2 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 Hathakas · Sep 21, 2015 at 11:50 PM 0
Share

Thank you so much! That totally worked!

avatar image

Answer by Budoray · Sep 22, 2015 at 03:45 AM

I may be misunderstanding your question, but in the event that I'm not, here is an example that uses Linq to show another way to tackle your problem. Your question read to me as if you only wanted to do somet$$anonymous$$ng if only one true element was in an array and to do somet$$anonymous$$ng else otherwise. The above for loop runs the risk of changing the toggled flag back to false if the last element happens to be false.

In your case, the test would be more like if(buildOptions.Any(x => x.isOn) ... or if you're just looking for the count to be one and only one, if(buildOptions.Count(x => x) == 1).

 using System;
 using System.Linq;

 public static void Main()
 {
         var n1 = new bool[3] { true, true, true };
         var n2 = new bool[3] { true, false, false };
         var n3 = new bool[3] { false, false, false };
 
         if (n1.Any(x => x)) // short for (x => x == true)
         {
             Console.WriteLine("N1 has at least one that is true");
         }
         
         if (n2.Any(x => x))
         {
             Console.WriteLine("N2 has at least one that is true");
         }
         
         if (n3.All(x => !x)) //short for (x => x != true)
         {
             Console.WriteLine("N3 has none that are true");
         }
         
         // Count
         var n1TrueCount = n1.Count(x => x);
         var n2TrueCount = n2.Count(x => x);
         var n3TrueCount = n3.Count(x => x);
         
         Console.WriteLine("N1 True Count: " + n1TrueCount);
         Console.WriteLine("N2 True Count: " + n2TrueCount);
         Console.WriteLine("N3 True Count: " + n3TrueCount);
         
         
         //Testing Count
         if(n1.Count(x => x) == 3)
         {
             Console.WriteLine("Do T$$anonymous$$s");
         }
 }
Comment
Hathakas

People who like this

1 Show 7 · 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 flashframe · Sep 22, 2015 at 12:55 PM 0
Share

Just to clarify, my solution doesn't run the risk of setting the flag to false if the last element happens to be false.

avatar image Budoray flashframe · Sep 22, 2015 at 01:16 PM 0
Share

No. It won't set it to false. I said it is risky. Using a for loop in the manner you showed is risky and inferred that care must be taken to manage that risk. My intent was not to challenge your programming ability or style. We all write code a bit differently. My apologies if I have offended you.

avatar image Hathakas · Sep 22, 2015 at 02:56 PM 0
Share

Thank you for the help! I'm still very new at programming so I'm happy to learn a different route. The first answer seems to work for what I need, but if you don't mind clarifying the risk factor so I can learn from it. Also, the script seems quite complicated lol but I wan't to know more about it. Can you refer me to were I can read up on it?

avatar image Budoray Hathakas · Sep 22, 2015 at 03:18 PM 0
Share

The risk is that the code uses a foreach loop to set a variable outside the loop with no break or way to exit the for loop until it loops through every item. Again, this isn't necessarily wrong. It's something that you have to be aware of and manage.

Take the code I shared and paste it into dotnetfiddle.net. Manipulate the code. Try other things with the code. The snippet of code I shared is not what you would use in your code. It only demonstrates how to use linq in a similar manner as a solution to your question. For testing and learning, replace n1, n2, n3 with buildOptions1, buildOptions2, buildOptions3. Once you get a grasp of what's going on with the snippet I shared, then you can go back to your code and change it if you wish.

if(buildOptions.Any(x => x.isOn) // same as if(buildOptions.Any(x => x.isOn == true) { // Do something }

If buildOptions has any toggle (x) such that (=>) toggle.isOn Do something

Does that help? Here's another primer for linq. http://www.dotnetperls.com/linq

avatar image Hathakas Budoray · Sep 22, 2015 at 10:08 PM 0
Share

Thank you for the information! I've been meaning to learn about Linq! I think I'm starting to get it. Really appreciate it.

Show more comments
avatar image flashframe · Sep 22, 2015 at 03:02 PM 0
Share

@Budoray No offence taken. I appreciate my comment was a bit curt, sorry. I too would like to know the risk if you could clarify.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Custom inspector array issues for loop 0 Answers

Why does this loop throw an error if int questComplete rises above 1? 0 Answers

Converting a string name to a sprite 2 Answers

Array problem, I'm novice 1 Answer

Remove empty "" strings from array String[] 1 Answer


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