Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make a quit button work?

Discussion in 'Scripting' started by SirToxic, Jan 13, 2017.

Thread Status:
Not open for further replies.
  1. SirToxic

    SirToxic

    Joined:
    Jan 13, 2017
    Posts:
    6
    Hi! I am brand new to Unity, or coding at all (other than HTML). I am making a menu for the roll a ball tutorial, and everything works except the quit button. I am following https://unity3d.com/learn/tutorials/topics/user-interface-ui/creating-main-menu to create the menu, but changing the bottom button to quit the game. I am using this code that does not work (C#):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Quitscript : MonoBehaviour
    6. {
    7.  
    8.     void QuitGame()
    9.     {
    10.         Application.Quit();
    11.         Debug.Log("Game is exiting");
    12.     }
    13. }
     
    pebarodigital likes this.
  2. SirToxic

    SirToxic

    Joined:
    Jan 13, 2017
    Posts:
    6
    BEFORE HATING ON ME! I have read other forums and none work.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Application.Quit() doesn't do anything when playing in the Editor. Have you done a build and tried it?
     
  4. SirToxic

    SirToxic

    Joined:
    Jan 13, 2017
    Posts:
    6
    yup. Just did it and nothing happend.

    in the "On Click ()" window i have put the Quitscript in the bottom, but i read that i don't need to add anything in the function part. Is this correct?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'm not sure what you mean. You have to add QuitScript.QuitGame to the list of button click events in the Inspector or do it via code at run time.
     
    LiterallyJeff likes this.
  6. SirToxic

    SirToxic

    Joined:
    Jan 13, 2017
    Posts:
    6
    How do i do this?
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  8. SirToxic

    SirToxic

    Joined:
    Jan 13, 2017
    Posts:
    6
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Make QuitGame public
    Code (csharp):
    1.  
    2. public void QuitGame()
    3.  
     
  10. SirToxic

    SirToxic

    Joined:
    Jan 13, 2017
    Posts:
    6
    Thanks! But when i continued wathcing the guide he added a quit button that also works when in the editor. Here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class QuitOnClick : MonoBehaviour {
    6.  
    7.     public void Quit ()
    8.     {
    9. #if UNITY_EDITOR
    10.         UnityEditor.EditorApplication.isPlaying = false;
    11. #else
    12.         Application.Quit ();
    13. #endif
    14.     }
    15.  
    16. }
    17.  
     
    FrOztDev and eoghanjdonnelly like this.
  11. zenfone234

    zenfone234

    Joined:
    Mar 26, 2019
    Posts:
    1
    its already work bro, u need to build and run it in ur PC!
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Yes, I'm sure they'll value this input two and a half years later.....bro....
     
    orionsyndrome and techyesbristol like this.
  13. BIGMANCOCO

    BIGMANCOCO

    Joined:
    Mar 27, 2020
    Posts:
    1
    I can't get anything to work!!!
     
  14. umarjarral39

    umarjarral39

    Joined:
    Mar 18, 2020
    Posts:
    1
    it is no work properly in game
     
  15. VictoriaPotts

    VictoriaPotts

    Joined:
    Apr 2, 2020
    Posts:
    1
    If anyone's still wondering what the solution is, there's nothing wrong with the code, that works fine. But you need to make an empty game object and put the quit script onto that by itself.

    And then in the OnClick part of the button, put the game object that has the script attached to it.

    After that, you should be able to click the "no function" part and find what you're looking for.
    (The name of your script, and then the name of the void function. Example: QuitScript -> QuitGame() then it should work.)

    I think your problem was that you were putting the script straight from your ProjectFolders in the OnClick, instead of a game object.

    (This also works for restart scripts, and I think scripts in general when you're trying to attach them to the OnClick).
     
  16. pebarodigital

    pebarodigital

    Joined:
    Aug 8, 2020
    Posts:
    5
     
  17. pebarodigital

    pebarodigital

    Joined:
    Aug 8, 2020
    Posts:
    5
    I don't know why other people have struggled to get your code to work, I tried it and it worked first time perfectly. It's a really simple solution so I guess the people complaining must have done something wrong or missed something out.

    Personally I say thank you as this really helped me out and finished off my game nicely!
     
  18. pebarodigital

    pebarodigital

    Joined:
    Aug 8, 2020
    Posts:
    5
    I've just said that I think your solution works perfectly, it did for me anyway so I have no idea why some people have posted negative comments. I wanted to say thank you for helping as this finished my game nicely.
     
  19. pebarodigital

    pebarodigital

    Joined:
    Aug 8, 2020
    Posts:
    5

    This is how I did it:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class QuitGame : MonoBehaviour
    7. {
    8.  
    9.     public void QuitTheGame() {
    10.         Application.Quit();
    11.     }
    12.  
    13. }
    14.  
     
  20. pebarodigital

    pebarodigital

    Joined:
    Aug 8, 2020
    Posts:
    5
    I couldn't understand why some people weren't able to get this working as it worked perfectly for me. The only difference was that I added the 'public' keyword before 'void' when writing my function. Like you I assigned the script to an object which in my case was the button I used to trigger the Application.Quit(); function call.
     
  21. itsoutchy

    itsoutchy

    Joined:
    Jun 12, 2021
    Posts:
    10
    This should work
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Quitscript : MonoBehaviour
    6. {
    7.     public void QuitGame() {
    8.      Application.Quit();
    9. }
    10. }
    Let me know if it doesn't work
     
  22. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Did you just necro a post to effectively post the same code that OP did 5 years ago?
     
    orionsyndrome, Kurt-Dekker and eses like this.
  23. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This reply ^ ^ ^ makes me think that the underlying problem may be an easy inspector error to make, not so much the code:

    Unity button onclick function script callback gameobject notes:

    https://forum.unity.com/threads/onclick-no-functions-shown.669334/#post-7328488

    For sliders and dropdowns and other value-returning UI elements:

    https://forum.unity.com/threads/cant-return-a-value-for-drop-down-list.1171442/#post-7504738
     
  24. NearRaindrop116

    NearRaindrop116

    Joined:
    May 26, 2023
    Posts:
    3
    you need to add a EventSystem and then go into the inspector of the EventSystem and do what it wants you to do
     
Thread Status:
Not open for further replies.