Newbie question about variables in C#

Discussion in 'Scripting' started by Idea, Oct 8, 2011.

  1. Idea

    Idea

    New Member

    Joined:
    Jun 2, 2011
    Messages:
    5
    Hello, My game is almost done but in the last minute I saw some problems with the pause menu, I was using a free pause menu I got from the asset store but I decided to make my own pause menu, So I finished it and debugged it and then I got this error

    Assets/Pause Menu/pausemenu.cs(5,17): error CS0825: The contextual keyword `var' may only appear within a local variable declaration

    I've tried switching my variable to a boolean and an int but it just brought more problems when I did that
    Here's the code I wrote

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class pausemenu : MonoBehaviour {
    6.     private var Pauseenabled;
    7.    
    8.     void OnGUI(){
    9.         if (Pauseenabled == true){
    10.             if (GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50),"Main Menu")){
    11.                 Application.LoadLevel("Main Menu");
    12.             }
    13.     }
    14. }
    15.  
    16.     // Use this for initialization
    17.     void Start(){
    18.         Pauseenabled = false;
    19.         Time.timescale = 1.0F;
    20.         Screen.showcursor = false;
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         if(Input.GetKeyDown("escape")){
    27.             if (Pauseenabled == true){
    28.                 Pauseenabled = false;
    29.                 Time.timescale = 1.0F;
    30.                 Screen.showcursor = false;
    31.             }
    32.         }
    33.            
    34.         else if (Pauseenabled == false){
    35.                 Pauseenabled = true;
    36.                 Time.timescale = 0.0F;
    37.                 Screen.showcursor = true;
    38.    
    39.     }
    40.   }
    41. }
  2. fholm

    fholm

    Member

    Joined:
    Aug 20, 2011
    Messages:
    1,626
    You need to specify a type, bool is what you need.
  3. samim

    samim

    New Member

    Joined:
    Jan 4, 2009
    Messages:
    13
    private bool Pauseenabled = false;
    privat int myint = 1;
  4. flamy

    flamy

    New Member

    Joined:
    Feb 7, 2011
    Messages:
    194
    declare it as bool it will be fine

    var will work only on js


    c# is more like c++, the syntax of declaration changes completely.
    Last edited: Oct 8, 2011
  5. fholm

    fholm

    Member

    Joined:
    Aug 20, 2011
    Messages:
    1,626
    Also note that var works in C# inside methods, just not in class field declarations.
  6. Idea

    Idea

    New Member

    Joined:
    Jun 2, 2011
    Messages:
    5
    I changed it to a bool but theres this whole bunch off errors like
    Assets/Pause Menu/pausemenu.cs(9,40): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
    Assets/Pause Menu/pausemenu.cs(9,33): error CS1502: The best overloaded method match for `UnityEngine.GUI.Button(UnityEngine.Rect, string)' has some invalid arguments
    Assets/Pause Menu/pausemenu.cs(9,33): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Rect'
    Assets/Pause Menu/pausemenu.cs(18,22): error CS0117: `UnityEngine.Time' does not contain a definition for `timescale'
    Assets/Pause Menu/pausemenu.cs(19,24): error CS0117: `UnityEngine.Screen' does not contain a definition for `showcursor'


    my current script



    Code (csharp):
    1.  
    2. public class pausemenu : MonoBehaviour {
    3.     bool Pauseenabled;
    4.    
    5.     void OnGUI(){
    6.         if (Pauseenabled == true){
    7.             if (GUI.Button(Rect(300,300,100,80),"Main Menu")){
    8.                 Application.LoadLevel("Main Menu");
    9.             }
    10.     }
    11. }
    12.  
    13.     // Use this for initialization
    14.     void Start(){
    15.         Pauseenabled = false;
    16.         Time.timescale = 1.0F;
    17.         Screen.showcursor = false;
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.         if(Input.GetKeyDown("escape")){
    24.             if (Pauseenabled == true){
    25.                 Pauseenabled = false;
    26.                 Time.timescale = 1.0F;
    27.                 Screen.showcursor = false;
    28.             }
    29.         }
    30.            
    31.         else if (Pauseenabled == false){
    32.                 Pauseenabled = true;
    33.                 Time.timescale = 0.0F;
    34.                 Screen.showcursor = true;
    35.    
    36.     }
    37.   }
    38. }
    39.    
  7. rokstar234

    rokstar234

    New Member

    Joined:
    Mar 29, 2011
    Messages:
    94
    change

    Screen.showcursor

    to

    Screen.showCursor


    also change

    Time.timescale

    to

    Time.timeScale


    Also where you have defined your Button you didn't put a new before the Rect. put one in and all should one sweetly

    this leaves yur code as
    Code (csharp):
    1.     public class pausemenu : MonoBehaviour
    2.     {
    3.         bool Pauseenabled;
    4.  
    5.         void OnGUI()
    6.         {
    7.             if (Pauseenabled == true)
    8.             {
    9.                 if (GUI.Button( new Rect(300, 300, 100, 80), "Main Menu"))
    10.                 {
    11.                     Application.LoadLevel("Main Menu");
    12.                 }
    13.             }
    14.         }
    15.  
    16.         // Use this for initialization
    17.         void Start()
    18.         {
    19.             Pauseenabled = false;
    20.             Time.timeScale = 1.0F;
    21.             Screen.showCursor = false;
    22.  
    23.         }
    24.  
    25.         // Update is called once per frame
    26.         void Update()
    27.         {
    28.             if (Input.GetKeyDown("escape"))
    29.             {
    30.                 if (Pauseenabled == true)
    31.                 {
    32.                     Pauseenabled = false;
    33.                     Time.timeScale = 1.0F;
    34.                     Screen.showCursor = false;
    35.                 }
    36.             }
    37.  
    38.             else if (Pauseenabled == false)
    39.             {
    40.                 Pauseenabled = true;
    41.                 Time.timeScale = 0.0F;
    42.                 Screen.showCursor = true;
    43.  
    44.             }
    45.         }
    46.     }
    Last edited: Oct 9, 2011