Cant Loop through GameObjects Children

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

Got a solution:

private bool DuplicateName(string Name)
    {
        bool Duplicate = false;
        GameObject objUser = UserPrefab;

        for (int i = 0; i < UserContainer.transform.childCount; i++)
        {
            try
            {
                string UserName = UserContainer.transform.GetChild(i).gameObject.GetComponentInChildren<Text>().text;
                bool b = clients.Any(UserName.Contains);

                if (b) { Duplicate = true; }
            }
            catch (Exception ex)
            {
                Debug.Log("Duplicate Error: " + ex.Message);
            }           
        }

            return Duplicate;
    }

You should consider fixing this error. It’s the single simplest error to fix. Just test if it’s null.

But your line 8 code will need refactoring, as it is technically considered compiler abuse and may make some sensitive software engineers cry.

How to break down hairy lines of code:

http://plbm.com/?p=248

Also, you really don’t want to do this:

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!

Haha that’s new one for me https://wiki.c2.com/?PokemonExceptionHandling

And in turn, from that link, I’d never heard the Diaper Pattern!

My goodness, I’ve had to un-Pokemon and/or un-Diaper so much code!! So bad to have mysterious bugs.

1 Like

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.

1 Like