- Home /
how do i check if a list contains elements from another list?
Hi everyone. I'm doing a app in unity using database and i need to check the information (string) conteined in my database to set some toggles on. How can I do that? I'm stuck on something like this:
public List allExercises = new List<GameObject>();
public string[] exercisesSplitString;
public void LoadData(){
foreach(string c in exercisesSplitString)
{
Debug.Log("length: " + exercisesSplitString.Length);
Debug.Log("c: " + c);
}
}
i need to check if there's any matching elements from 'exercisesSplitString' and 'allExercises' (the object names) so I can turn on the toggles from the elements in 'allExercises' that matches.
hope that makes sense. that's not the entire code but i think it'll do. i'm using the debugs just to check if the foreach is working properly because it wasn't.
thank you!
Answer by BartekWilk · Jun 16, 2020 at 04:53 PM
Just iterate over array and check if "allExercises" has got any value that is in "exercisesSplitString" by function with for loop (foreach loop is slower):
private bool ArrayValueInList()
{
for(int i = 0 ; i < exercisesSplitString.Length ; i++)
{
if(allExercises.Contains(exercisesSplitString[i]))
return true;
}
return false;
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Iterating through nulls in legacy-style Animation component 0 Answers
Save / Load List with Prefabs instantiated in the runtime 2 Answers
How do I find a local variables index in a foreach loop? 1 Answer
How would I find the name of all game objects in a c# List 1 Answer