Not sure what I’m doing wrong here. I need to loop through all the children game objects inside this container game object. Its crapping out on Client Name part:
private bool DuplicateName(string Name)
{
bool Duplicate = false;
GameObject objUser = UserPrefab;
for (int i = 0; i < UserContainer.transform.childCount; i++)
{
string ClientName = UserContainer.transform.GetChild(i).gameObject.GetComponent<Text>().text;
bool b = clients.Any(ClientName.Contains);
if (b) { Duplicate = true; }
}
return Duplicate;
}
Did not wrap it in a try catch so no error given. But I assume I’m just doing something simple wrong. I’ve never tried to iterate through game objects children before. Ultimate goal here is to see if a object with this name already exists. If so then its a duplicate and return true. I also will need similar code to this for another function. Trying to kill two birds one stone with this questions. Thanks in advance
It errors right here:
string ClientName = UserContainer.transform.GetChild(i).gameObject.GetComponent<Text>().text;
bool b = clients.Any(ClientName.Contains);
Update:
I wrapped it in a try Catch
error: Object reference not set to an instance of an object
That’s called “Pokemon Exception Catching” and is extremely dangerous, because if there is ANY error whatsoever in the try {} block, you’ll never hear of the error, it’s just swallowed and destroyed silently, which could lead to Very Bad Things™ later on in your program.
But hey, it’s your code! Go nuts. It is after all almost the year 2021. HAPPY NEW YEAR!
You should learn how to debug code. Step through your code, inspect variables and understand what the program is doing with that data. Once you learned how to debug code, there is almost no reason to ever ask how to fix an error again.
I found this Unity and Visual Studio “How to debug” video for you. If you use a different IDE, I’m sure you can find plenty of tutorials on youtube for it as well.