Why is "Selected" of type GameObject and not of type Image?

Hello,
i have some trouble understanding why i can’t get “Selected”-object as an Image-type and set image.enabled to true or false like we did in the GridSystemVisualSingle.

We got the button of type Button by draging the ActionButtonUI into the SerializedField in the Inspector so we could add the Listener to the onClick-Event.
We could reference the TextMeshProUGUI Component by draging the “Text”-childObject to the Inspector and then directly set the text.

[SerializeField] private TextMeshProUGUI textMeshPro;
{
    textMeshPro.GetComponent<TextMeshProUGUI>().text = baseAction.GetActionName().ToUpper();

}

It is also possible to get a reference of type “GameObject”, then get the Component “TextMeshProUGUI” and then set the text.

[SerializeField] private GameObject textGameObject;
{
    textGameObject.GetComponent<TextMeshProUGUI>().text = baseAction.GetActionName().ToUpper();

}

So why can’t i reference the Image Component directly and disable it?

[SerializeField] private  Image selectedImage;
   
 public void HideSelectedImage(){
   selectedImage.enabled = false;}

Even when i have the GameObject reference i can’t get the Image-component and disable it.

[SerializeField] private  GameObject selectedGameObject;
   
 public void HideSelectedImage(){
   selectedGameObject.GetComponent<Image>().enabled = false;}

I know that when I have the GameObject-reference I can call the SetActive()-methode, but shouldn’t my initial solution work aswell?

Welcome to the community @Xinegy

That’s a very good question. Both those options work for me. Are you referencing the correct Image type? You should have using UnityEngine.UI; at the top. I know there are some other namespaces that also have Image types, like Microsoft.Unity.VisualStudio.Editor for instance

Yup like @bixarrio said both methods should work, what exactly doesn’t work?
Disabling the game object will disable all components, disabling the image only disables the image component, if that’s the only component on the object then both do basically the same thing

1 Like

After I copied the code from the lecture to continue with the course and now rewrote my initial Solution it somehow works without a problem.

ActionButtonUI:

[SerializeField] private TextMeshProUGUI textMeshPro;
[SerializeField] private Button button;
[SerializeField] private Image selectedImage;
private BaseAction baseAction;

public void SetBaseAction(BaseAction baseAction)
{
    this.baseAction = baseAction;
    textMeshPro.text = baseAction.GetActionName().ToUpper();
    button.onClick.AddListener(() => {
        UnitActionSystem.Instance.SetSelectedAction(baseAction);
    });
}

public void HideSelectedVisual()
{
    selectedImage.enabled = false;
}

public void ShowSelectedVisual()
{
    selectedImage.enabled = true;
}

public BaseAction GetBaseAction()
{
    return baseAction;
}

UnitActionSystemUI:

private void UpdateSelectedVisual()
{
    BaseAction baseAction = UnitActionSystem.Instance.GetSelectedAction();

    foreach (ActionButtonUI actionButtonUI in actionButtonUIList)
    {
        if(actionButtonUI.GetBaseAction() == baseAction)
        {
            actionButtonUI.ShowSelectedVisual();
        }
        else
        {
            actionButtonUI.HideSelectedVisual();
        }

    }
}

But I have a follow up question: Like I said I tried to have the same structure we made for the GridSystemVisual and GridSystemVisualSingle which is basically the same starting situation. There we handled the logic for showing and hiding in the prefab-initializing-script (GridSystemVisual) while the script on the prefab (GridSystemVisualSingle) only has the methods Show() and Hide().
For the ActionButtonUI we handle the show/hide logic on the Prefab-script directly.

Is there a specific reason for doing it differently this time? There was a similar question with the answer that it’s mostly preference, but now we have both approaches in the same project.

For the Action button, the visual show/hide depends on the underlying BaseAction which the button has a reference to, so it makes sense for the button to use the info it already has in order to decide its state.

Whereas on the Grid Visual, each individual Grid Visual Single doesn’t have any extra data, it’s just a dumb visual, it does not know what is the selected action, what is the selected unit, etc. So it makes sense for an external script that does have that information to just tell it what to do.

But as usual there’s a million ways to solve the same problem, so making the UnitActionSystemUI decide which button to show or hide also works just fine.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms