Accessing Button From Prefab

Hi,
(new to Unity disclaimer)
I have a Prefab called DisplayWord that contains a child Button and a child Tile object (from another project). I’m using Instantiate to create copies of DisplayWord. I need to access the button so I can add an OnClick eventListener. I’m able to instantiate DisplayWord and I can access the Tile, but I can’t access the Button from the instantiated object. I read a few posts regarding some limitations on Prefabs with Buttons so I don’t want to spend much more time if this can’t be done. The Tile does have a Tile script, not sure if that’s why I am able to access it. It looks like you can’t add a script to a button via addComponent, just in the onClick. I’ve tried various permutations of initializing the Button in my DisplayWord script. Again, the script gets the tile but not the button.

My goal is to have this word/tile/text field with a button attached to it. The button will pass the contents of the text field. I was able to get this done using just a Button prefab but just can’t seem to get a button and text combo.

Any guidance would be appreciated. I’ve spent a few hours already.

var newDisplayWordx = Instantiate(newDisplayWord, new Vector3(0, 0, 0), Quaternion.identity);
newDisplayWordx.transform.SetParent(transform, false);
var dw = newDisplayWordx.GetComponent();
var dwb = dw.GetComponent(); // returns null

You said the button is a child. So using GetComponent<T> on the top level object isn’t going to find it. You probably want to be using GetComponentInChildren<T> instead.

Alternatively on your DisplayWorld component, you can serialize a reference to it’s child button, assign that in the inspector, and expose a property to access it.

Thanks for your responce… I had tried GetComponentInChildren. It didn’t work. I suspect that I need to use one of the FindGameObject() methods to find a button since it appears that you can’t add a script to a button and GetComponentXXX() is looking for script components. Not 100% certain of that as I am new to Unity.

I am creating buttons at runtime so assigning in the inspector is not an option.

I will experiment with adding my button to an empty GameObject and I can add the script to that.

You can absolutely add components to a button. A Button is just a component itself, on a game object. Game objects are just containers for any combination of components.

Where is this button meant to be then? Because it sounds like it’s on a completely different game object with no relation to the one you’re instancing.

Thanks again. I do see now that I can add a script to the button,

I still don’t see how to modify the text. I did manage to get the TextMeshPro gameobject but was unable to cast it or modify it.

var myButton = emptyGameObject.transform.GetChild(0).gameObject;
var myButtonText = myButton.transform.GetChild(0).gameObject;

Again, game objects are containers for components. They are not the components, they just store them. That’s why we have GetComponent<T> and its variations. You were probably using GetComponentInChildren<T> on the wrong game object. Make sure you’re using the right type as well, such as not confusing UnityEngine.UIElements.Button with UnityEngine.UI.Button.

Again, appreciate your taking the time to look at this.

Getting closer but still not there. I attached a script to the button and can see that I’m where I want to be in the debugger. The button has a child which displays as Text (TMP) . Trying to access the text but getting null. Have tried class TextMeshProUGUI, TextMeshPro, Text, etc

text = GetComponentInChildren();

Tried again with TextMeshProUGUI and this time it found it. Thanks for all your time and help.

1 Like