Search Game Inventory Button Object Text Strings

Howdy yall,
My inventory system is a bunch of buttons in layout groups. I have a search input field, but I am having trouble capturing the string there… and Unity isn’t “pointing straight at my problem.”

This works to report all the button items’ labels:

    void UserSearchButton_onClick()
    {
        print("User Search: " + userSearchText.text);
        foreach (Transform child in activatedMenuTransform.transform)
        {
            print("Text Object Name : " + child.gameObject.GetComponentInChildren<Text>().name);            
        }
    }

But this reports
“NullReferenceException: Object reference not set to an instance of an object”

void UserSearchButton_onClick()
    {
        print("User Search: " + userSearchText.text);
        foreach (Transform child in activatedMenuTransform.transform)
        {
            if (child.gameObject.GetComponent<Text>().text.Contains(userSearchText.text))
            {
                print("Text Object Name : " + child.gameObject.GetComponentInChildren<Text>().name);
            }
        }
    }

I also messed with
if (child.gameObject.GetComponent().text.IndexOf(userSearchText.text) >1)
but I get the same error.

Any ideas would be appreciated!

I notice that you’re calling GetComponentInChildren on child to reference the Text component in the print statement, but in the branch you added you’re just calling GetComponent. I don’t know your object hierarchy, but if child doesn’t have a Text component then that would explain the null reference.

Thank you, thank you, thank you… I will try not to post proofreading questions frequently! To avoid tripping on that… I should probably get a looser with adding variables…

public void UpdateUserSearch()
    {
        print("User Search: " + userSearchText.text);
        print("activatedMenuTransform.transform: " + activatedMenuTransform.transform.name.ToString());
        var i = 0;
        foreach (Transform child in activatedMenuTransform.transform)
        {
            var myText = child.gameObject.GetComponentInChildren<Text>().text;
            if (myText != "Prefab'sText")
            {
                if (myText.Contains(userSearchText.text.ToString()))
                {
                    print("Item #" + i + " reports..." + "

" +
"1st Text Object Name : " + child.gameObject.GetComponentInChildren().name + //Returns name of Text Objects
" | Item Name: " + myText); //Returns content of Text Objects
}
}
i++;
}
}

This works awesome… now to flush out some features!

Thanks, again!
Excellent username @alwayscodeangry! Very relatable.