UI Outline Enable / Disable via Script?

So I have a basic main menu and subsequent sub-menu. I have pointerEnter set to enable an outline on my text, to show that it’s actively “selected”. I have pointerExit set to disable outline on exit.

I have my menu script attached to the canvas, where all of the buttons / text live. I have the two panels (main menu, options) set to groups in my script so i can show / hide said panels when the appropriate button is clicked (options button, back button).

I want to deselect (turn off the outline) of a button when it’s clicked, so that returning to that screen shows no button currently selected.

I have tried numerous things to see if I could get my script working. At this point my only option is to set each and every button to a variable, but I was hoping I could just grab all outlines (or grab all buttons and find their text / outline component), then turn them off either in a loop (foreach component outline in menugroup) or simply by telling unity to turn off all outlines (kind of like how jQuery can affect all objects in an array at once).

I’m strictly using C# for this learning project, so if you can point me in the right direction, it’d be very helpful.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class menuScript : MonoBehaviour
{
    public GameObject mainPanel;
    public GameObject optionsPanel;

    private Outline outline;

    void Awake()
    {
        mainPanel.SetActive(true);
        optionsPanel.SetActive(false);
    }

    public void exitGame()
    {
        #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
        #else
            Application.Quit();
        #endif
    }

    public void showOptions()
    {
        mainPanel.SetActive(false);
        optionsPanel.SetActive(true);
    }

    public void showMain()
    {
        mainPanel.SetActive(true);
        optionsPanel.SetActive(false);
    }
    public void inactiveOutlines()
    {
        outline = mainPanel.GetComponentInChildren<Outline>();

        Debug.Log(outline.IsActive());
        Debug.Log(outline);
    }
}

Outline outline;

	outline.enabled = false;

OK, so after getting some sleep, seeing GilbertoBitt2’s answer, and doing a little more playing around, I came up with the following:

    private Outline[] outline;
    private Outline[] outline2;

    public void inactiveOutlines()
    {
        outline = mainPanel.GetComponentsInChildren<Outline>();
        foreach(Outline o in outline)
        {
            o.enabled = false;
            Debug.Log(o.name);
        }
        outline2 = optionsPanel.GetComponentsInChildren<Outline>();
        foreach (Outline o in outline2)
        {
            o.enabled = false;
            Debug.Log(o.name);
        }
    }

I just call my function onClick() of each button, and it takes care of its siblings. Hopefully this helps others who get stuck.

Gilberto is calling out the difference between GameObjects and Components.

A GameObject can have many components. Transform, Image, Button, Outline, etc.

To turn an entire GameObject on & off, use MyGameObject.SetActive().
To turn just one component on & off, use MyComponent.enabled.

As far as turning off the outline on all the buttons, that’s sort of up to you.
You might want to look into GetComponentsInChildren<>(). note plural “Components”.