UI Buttons will not change their image because the game object lacks an image definition.

Hello community,

I have been creating my first Unity game for android after coming over from the standard SDK. I am by no means fluent in C# but i have had a fair amount of experience with Java. But back to the task at hand; i am creating an app with a grid of 9 buttons every time one of the buttons is clicked a random number is generated(this is not displayed to the user), When that number is equal to a certain number it changes the image of the button that was clicked.

this is what i have so far on the script relating to this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class BF : MonoBehaviour {

    private int rand;
    public Sprite Goldlayer;

    public void OnButtonClick()
    {
        var gotit = EventSystem.current.currentSelectedGameObject;
        rand = Random.Range(1, 10);

        if (gotit != null)
        {
            Debug.Log(gotit.name);
            if (rand == 1)
            {
                gotit.image.sprite = Goldlayer;

               
            }else{}
        }else {}  
    }
}

So I have attached this script to the camera so when any of the buttons are clicked it will know which one(this is contained in gotit) is clicked. The issue i am having is that i can not change the image of the clicked button because it doesn’t have an “image definition”. I dont know why it doesn’t, i would of thought it would be like any other game object.

If you have any ideas or suggestions i would greatly appreciate it.

Kind and thankful Regards.

1 Answer

1

So, accessing the image of your button isn’t done with an image property (or field). If it’s there, it’s deprecated or obsolete. Instead, you’ll write

gotit.GetComponent<Image>().sprite = Goldlayer;

Problems can arise, however, if gotIt doesn’t contain a value (difficult for that to happen given your assignment and that buttons generally do become selected when clicked), but that’s far more complicated than I really need to explain. Fact is, you’ll want to prevent race conditions by ensuring that gotit has an image component, so:

if (gotit.GetComponent<Image>())
    gotit.GetComponent<Image>().sprite = Goldlayer;

Doing it this way prevents a whole world of headaches later on. It’s not enough to verify that the object itself exists, you need to make sure that the thing attached to the object that may or may not exist also exists with that object. To make your life easier, you can attach this script to the button itself and stick a RequiredComponent attribute at the beginning of the class, which tells the button to add an Image component to itself (cool, right?), so:

[RequiredComponent(typeof(Image))]
public class SomeClassThatNeedsAnImage : MonoBehaviour {
    Image m_myImage;

    void Awake() {
        m_myImage = GetComponent<Image>();
    }
}

Hey, that thing I said about race conditions? You don’t have to worry about it now! So, yeah, take some time to learn some C# and familiarize yourself with Unity’s API. Good things will happen - just ask the guys on madewith.unity.com

Thanks so much man really helpful and thanks for taking the time with my question. Hope you have a good one.