Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Quit button not working?

Discussion in 'Scripting' started by tyrumma, Apr 16, 2021.

  1. tyrumma

    tyrumma

    Joined:
    Mar 17, 2018
    Posts:
    3
    Preface, I have absolutely no programming experience so ELI5
    And yes I've built the game, run the exe and the button does not work there.
    It appears like it's working, the colours change when mousing over and clicking but when it is clicked- nothing happens.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PauseMenu : MonoBehaviour
    6. {
    7.  
    8.     public void QuitGame()
    9.     {
    10.  
    11.         Application.Quit();
    12.  
    13.     }
    14.  
    15. }
    16.  
    This is what my script looks like. I attached it to a GameObject in my scene, and then attached that object to the on click event for the button.
    upload_2021-4-16_11-17-1.png

    I've been trying to google search an answer for a couple of hours now but I can't get anything to work.

    Edit: I tried adding a debug log and, if I'm looking in the correct place and doing this correctly, it doesn't fire off when I click the button
     
    Last edited: Apr 16, 2021
  2. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    178
    Hello,

    If you put a Debug.Log in your quit game function, does it register?
     
  3. tyrumma

    tyrumma

    Joined:
    Mar 17, 2018
    Posts:
    3
    It doesn't seem to if I'm doing it right. I play in the editor, hit the button, check the console, and there is no debug message there.
     
  4. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    your script appears to work, when I put it on a game object and attached it to the on click event it worked fine. so it's not the script.

    question:
    1. what game object is the script on and what is the game object doing?
     
  5. tyrumma

    tyrumma

    Joined:
    Mar 17, 2018
    Posts:
    3
    It's just a low poly vehicle placed in the scene. It doesn't have a purpose or any other components attached to it.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    As a matter of style, it's best to centralize UI responder scripts, either to the root Canvas they belong under, or another GameObject where they can all live. Otherwise you will hate yourself trying to find them later.

    Also, Application.Quit() does NOTHING in the Unity editor.

    And also +1 to Magnesium's post above. We know buttons didn't stop working, so review what you need:

    - EventSystem
    - no "RaycastTarget" objects in front of the button
    - button must have a "Graphic" with RaycastTarget enabled
    - valid hookup of delegate (your image above)
    - etc.

    We know it works.
     
    Lenticularic likes this.