- Home /
Need some help with Array searching logic
I have an array of strings which is 42 in length. I also have an array of GameObjects which is 50 in length.
Inside a coroutine, I am picking a random int up to the length of the string array. I am then checking that string against the GameObjects name inside the GameObject array. I can easily figure out how to check if something exists, and if it does, restart the coroutine until it finds one that doesn't exist. The problem I have is that I cannot figure out the logic for if there are no more Strings in the array that DO NOT match the GameObject array from then I want to call another function. I have also tried moving this to a loop but whatever I try I am running into a stack overflow. I'm not the best at explaining things so I hope this makes sense, I will snippet what I think is important below.
IEnumerator _Continue()
{
int pickRandom = Random.Range(0, STRINGARRAY.Length);
if(GAMEOBJECTLIST.IndexOf(GameObject.Find("blah/blah/bah/blah/" + STRINGARRAY[pickRandom])) < 0) //Doesn't Exist
{
//DO STUFF HERE
}
if(GAMEOBJECTLIST.IndexOf(GameObject.Find("blah/blah/bah/blah/" + STRINGARRAY[pickRandom])) > 0) //If it does exist
{
StartCoroutine(_Continue()); //Restart the coroutine
}
}
It's here that I am getting stumped, I cannot seem to find a way to stop and run something else if All of the strings (which are the GameObject names) exist and therefore I no longer need to check.
Any help would be appreciated, I should mention that the GameOBject[] is a List and the String[] is an Array.
Answer by GrayLightGames · Oct 13, 2019 at 03:58 AM
I think what you're saying is that if all of the strings in your STRINGARRAY match GameObjects' names, you want to stop calling _Continue and do something else right? I don't know your architecture, but you could make a copy of the STRINGARRAY and use the copy to do this operation. You would then remove checked names from it on each iteration. So if it finds a GameObject name match, that name is removed from the array and it never checks that one again. Eventually, the STRINGARRAY will be empty, which is the case you're looking for. Your coroutine would first have a check if the STRINGARRAY is empty, so you could break out there, and if it's not empty, do what you have already coded. Follow? This would also cut down some of the redundancy as you're not randomly checking the same name twice. _Continue would be called 42 times only.
Thanks for the reply, that sounds like a good idea that should work, I'll remove each one from the string array once it is "used" and check if it's empty. Thanks!
Great glad I could help. Good luck with your project!