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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Please Help ! I want to be able to access my GUI Menu by pressing ESC (C#)

Discussion in 'Scripting' started by m3da, Aug 31, 2015.

  1. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Hi there,

    I am very new to programming and coding in Unity.

    I have been looking at existing GUI menu tutorials however none seem to address what I am trying to do.



    I have a GUI menu system which I have created in a scene called Main.

    Within this menu You can access different levels/scenes of my game/experience.

    Say I am at Scene 1 which doesn't have the menu system in there... a completely blank scene.

    I would like to access this GUI menu by pressing Escape on the keyboard to show it and escape for it to disappear. ((Just like a pause menu.)) -

    Note - I do not want to have to change scenes back to the main menu pressing escape, I would just need to be able to access the my GUI menu - from which I can access different scenes.


    I hope this outlines my objective what I want to accomplish and any help I would be very greatful.

    - I am primarily a 3D Artist with little programming experience.

    I am using C#


    Thanks very much in advance.

    Ben
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (CSharp):
    1. public GameObject guiObj
    2. Update() {
    3.   if(Input.GetKeyDown(Keycode.Escape)){
    4.     guiObj.SetActive(!guiObj.activeSelf);
    5.   }
    6. }
     
  3. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Hi Hpjohn

    Thanks for this.


    unfortunatly I have just tried the script you supplied and I am getting a

    Assets/pausemenu.cs(2,6): error CS8025: Parsing error

    Forgive me if you see something blatently wrong which I am unaware of.

    I have attached the script I used below to an empty game object in the new ( empty scene)

    Many thanks again for your patience.


    Ben


    error.jpg
     
  4. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    Your getting this error because you deleted the whole outline of the script! You should have at the top:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pausemenu : MonoBehaviour {
    5.       // Code then goes here
    6. }
    7.  
    I'd consider looking at some basic programming tutorials before doing your own thing! Look at the Unity learn section
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For C# you need to write the script out in full. Create a new script with the menus inside Unity, then paste this code inside the class.

    And there is a semi colon dropped off the end if the first line.

    I'd suggest doing roll a ball to start. It doesn't take long, but it should give you a quick feel for what a script must have.
     
  6. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Hi Georetro/Bored Mormon

    Thanks for this.

    I actually included it the first time but removed it as it wasnt included in hpjohns script he supplied.
    - this is my fault not his as i should have been able to recognise that it should have been included.

    And yes I have been trying to learn some basic C# in my spare time and I will continue to do so.

    I hope to someday return the favour to somebody here when I am like the 'rainman' of C#.
     
    georetro and Kiwasi like this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yeah, we have a habit of being lazy and not typing out the full using statements or class declarations when coding for the forum. It saves space and time, but occasionally catches new users out.
     
    georetro and m3da like this.
  8. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    thanks.. Going to try now and hopefully it works.

    Ben
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I have the habit of intentionally leaving things out so that people can exercise their own abilities to a degree
    it's no good spoonfeeding, you dont learn anything
    it's also missing a "void"
     
  10. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Guys.. I am very frustrated and Sorry here... With myself that is.


    I have followed both your commands and included the outline script at the top of the code.


    3 errors I am getting below:

    Assets/pause.cs(6,10): error CS1519: Unexpected symbol `Update' in class, struct, or interface member declaration

    Assets/pause.cs(6,5): error CS1520: Class, struct, or interface method must have a return type

    Assets/pause.cs(10,1): error CS8025: Parsing error

    I have double and tripled checked everything what Hpjohn advised and I cant see where I went wrong.
    I am a severe dyslexic so I presumed I would have miss-spelt something.

    from what I understand the parsing error should have been resolved as I included the outline at the top of the code error 2.jpg


    I am extremely sorry about this guys. really! Nothing more frustrating then being stuck on a small hurdle like this.


    Thanks a lot!!!!

    Ben
     
  11. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19

    I understand and I agree with you.

    The problem is when I look up the error codes myself, I see many different issues people are having with their lines of code which is difficult to read and know where they went wrong. I actually tried looking it up after you posted the code.

    Very sorry to take up your time.

    Ben
     
  12. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Believe me, I can hear your sighs all the way from Ireland.
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I have too much professional pride to leave mistakes in code with my name on it.

    @m3da add a semi colon at the end of line 5 and a void at the beginning of line 6. It's not your abilities, you were fed dodgy code to begin with.
     
  14. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    How about we throw some blame at that dodgy IDE called Mono Develop? Visual Studio's first error pointed out that missing-semi colon but from the screenshot given by the OP Mono Develop didn't seem to notice it.
     
    Kiwasi likes this.
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The error messages were clear enough. You just need to learn to speak MonoDevian. ;) Seriously, after a day in VBA these errors look highly descriptive.

    'I was kind of expecting something else here'

    'You need a void or something'

    'I give up'
     
    LaneFox and Ryiah like this.
  16. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    I've already closed VS2015, but the first three errors were something along the lines of "You missed a semi-colon", "You forgot to give this function a return type", and "You need to specify the namespace for those function calls".
     
    Kiwasi likes this.
  17. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Hi BoredMormon, I feel your frustration.. I really do.

    Followed all your points.. included the void and the semi colon.. fixxed everything and it plays.. however when i press Esc it doesnt bring up the Gui..

    Guess I will just have to pay somebody to do this as Iv'e been trying for nearly 2 hours now.
     
  18. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Did you put something in the guiObj slot? The script can't just magically find some object you want turned on without some guidance.
     
    m3da likes this.
  19. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Solved it



    using UnityEngine;
    using System.Collections;

    public class pause : MonoBehaviour {
    public GameObject guiObj;
    void Update() {
    if (Input.GetKeyDown (KeyCode.Escape)) {
    guiObj.SetActive (true);
    }
    }
    }
     
  20. m3da

    m3da

    Joined:
    Aug 31, 2015
    Posts:
    19
    Yes I did.. - Thanks for pointing it out to me.. You can never be to thorough with coding.. thats what Ive learnt :)



    Thanks for all your help..

    Hopefully if another novice runs into this issue this thread will save a lot of coders this frustration and they may find answers along the way.

    Benjamin