- Home /
Check if specific object exist in list or array, Best Practices?
What would be the best way to check if an object exist in a array or list using a loop.
If the object exist //do something.
if the object does not exist do something else. Thanks
The method below compares string names of the parameter 'effect.name' but is missing something or i do not fully understanding the check logic or iteration process.
public void CheckForStatusEffectInPool(Item effect, Transform recipient)
{
// Debug.Log(effect.name);
statusEffectPool = statusEffectPoolParent.GetComponentsInChildren<Transform>();
for (int i = 0; i < statusEffectPool.Length; i++)
{
if (effect.name == statusEffectPool[i].name)
{
Debug.Log("Is equal: " + statusEffectPool[i]);
statusEffectPool[i].transform.parent = recipient.root;
statusEffectPool[i].transform.Find("StatusEffect").gameObject.SetActive(true);
break;
}
Debug.Log("This should not fire in the console because of the break;");
}
}
the break; at the end appears to not escape the loop and neither does return; when replacing the break;.
Below is the image read out of the console when the method is ran. The method is ran only once with a trigger.
this is a shot of the inspector for the Status Effect pool array when GetComponentsInChildren() is called in the method.
Answer by Triqy · Oct 23, 2019 at 11:45 PM
This is the answer i was looking for. I got rid of the unity array and implemented a generic LIST array and then ran a check with a List.Contain(); method . The code is below.
public void CheckForStatusEffectInPool(Item effect, Transform recipient)
{
// Debug.Log(effect.name);
for (int i = 0; i < statusEffectPool.Count; i++)
{
if (statusEffectPool[i].name.Contains(effect.name))
{
Debug.Log("equal");
return;
}
else
{
Debug.Log("not equal");
}
}
Answer by ChuckIes · Oct 23, 2019 at 11:18 PM
Your loop is running twice: the first time it's looking at StatusEffectPool, and since it doesn't match the effect name, it skips your if block and hits the debug message. Then the second time it's looking at WeakHealthDrain and it triggers the if block. You can verify this by switching the order of your elements so WeakHealthDrain is inspected first and the loop breaks before it inspects StatusEffectPool.
If you want to execute code when the for loop fails to find a match, you can use a return instead of a break and place the code after the end of the for loop.
Thank you for your comment but i tried the return; and is still didnt break out the loop. I also check to see if the trigger was some how calling it twice and it was not.
I found the answer i was looking for by getting rid of the standard array and implementing a LIST variable. I then iterate through my transform and checked with List.Contains() method; with return; at the end. Check out the reply below from me for the full explanation
Your answer
Follow this Question
Related Questions
Weird issue with List and for loop. 1 Answer
Copy values between two classes in two lists. 1 Answer
Splitting String Into Array 2 Answers
Change part of a string [Solved] 3 Answers
How do I find a String in an Array? 2 Answers