I added an image to a button, how do i access it

I have a button with the following setup that i am using in a dynamic scroll list:


I have added the image component to get the following look to the button:


Depending on the number of players i want to change the image, the one added on the left, but i am just not get it how to do access the sprite.

I know that i should use GetComponent or GetComponentInChildren but i just can’t get it to work. I have tried quite a few different options. I am able to access the text, as cane seen in the code below. The way i initiate the button, in a dynamic scroll list, is the following:

//create a new item, name it, and set the parent
Button newItem = Instantiate(btn_Template) as Button;
newItem.name = theActiveTemplates*;*

newItem.transform.SetParent(gameObject.transform, false);
newItem.GetComponentInChildren().text = theActiveTemplates*;*


newItem.transform.FindChild(“Image”).gameObject.GetComponent().sprite = yourSprite;

Make sure yourSprite is a Texture of type Sprite (2D and UI)


newItem.transform.FindChild(“Image”).gameObject.GetComponent().sprite = sprt_fourPlayers;

This worked :slight_smile: …thanks @_Joe it is good to learn new stuff.

It seems like you have added an image, so to me it seems like you should simply do:

public Sprite someSprite;


newItem.GetComponentInChildren<Image>().sprite = someSPrite;