4.6 UI How to change an added image-sprite in a button

I have added an image into a button and is now trying to change that sprite and have problem to grab it.

newItem is a GameObject as i create the Button’s dynamically.

Button

  • Text
  • Image <<< Added this to the button

I am trying to do it with: newItem.GetComponentInChildren<Image>().sprite = sprt_twoPlayer;

but can’t actually grab the actual image, i get the button background. The result is that the button native image is change meaning that the background get’s the sprite. I want my little added image to get the sprite.

Anyone that could help me with some tips here?

That should work I just tested it myself. I’m not sure if you are repeatedly doing this, if so I suggest you cache the Image reference and the button so you can edit their properties without having to waste CPU time, its small CPU usage but try to save where you can so your game can run as smooth as possible.

It somewhat works for me as well but it change the Button image, my sprite is on the button but not on the image i have attached to the button.

The image i want to change is the small figure in the picture (the added image object), not the actual button.