- Home /
 
Reference all objects inside of an Array or List?
I've been developing a software with Unity that isn't really a game, but more of an education software and I've been messing around with Arrays and Lists... But I don't really know if this is a possibility.
But I need to know if you can reference all objects inside of an Array or a List.
         if (WrittenText1.text == Names1.Name[0])
         {
             Person1.SetActive(true);
         }
         else
         {
             Person1.SetActive(false);
         }
         if (WrittenText1.text == Names1.Name[1])
         {
             Person1.SetActive(true);
         }
 
               The code's getting way too big for something that could be adjusted by something like that, if I could reference every single string object with only 1 code line.
I have a series of Names, Actions and Places and typing such things results in their objects being turned on, or the objects doing their actions.
I don't know if anyone knows a workaround this, or if I'm being too dreamy with programming, but either way, it's the kind of pickle I ran into.
Answer by ShadyProductions · Aug 18, 2017 at 06:20 PM
How about with a simple foreach loop?
         foreach (var name in Names1)
         {
             if (WrittenText1.text.Equals(name, StringComparison.OrdinalIgnoreCase))
             {
                 // do whatever
             }
         }
 
              Answer by Numenization · Aug 18, 2017 at 06:23 PM
Use a for loop or a foreach loop. For example:
 foreach(Name name in Names)  // for each object of type "Name", referenced as "name", in the list "Names"
 {
 // code here
 }
 
 
               Or:
 for(int i = 0; i < Names.Count; i++) 
 {
 // code here
 // reference names by Name[i], where the result is the name at the i'th index
 }
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
How to get the name of a List from one of its items 1 Answer