Referencing Objects in the Hierarchy?

I’m trying to create a script the runs the Application.Quit() function when a UI button is clicked, but I seem to be unable to reference said button in the Hierarchy. I’m attempting to reference the “Exit” button. I used the code:

if (Canvas.Exit)
            {
                Application.Quit();
            }

My hierarchy is structured as:

  • Main Camera

  • Directional light

  • Canvas

  • PauseMenu

  • PauseInspector

  • Video Settings

  • Audio Settings

  • Exit // Button I want to reference

  • Text

  • EvenSystem

That’s not how UI buttons work, and this isn’t the right place for this question either, but whatever. If you want a UI button to run a code, then simply select it in scene view, you’ll see the settings of it, and at the bottom there will be a window for the actions, that should happen
To run the code, place it in a public method, and then place that code into a gameObject, and then reference the gameObject in the buttons action menu, and select the method

1 Like