Unity Community


Results 1 to 7 of 7

  1. Posts
    5

    Newbie question about variables in C#

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


  2. Location
    Sweden
    Posts
    1,418
    You need to specify a type, bool is what you need.
    My Open Source Projects: UdpKit, IronJS, Free Unity Assets
    Need unity consulting? Contact me over PM or Here


  3. Posts
    13
    private bool Pauseenabled = false;
    privat int myint = 1;


  4. Location
    Chennai,India
    Posts
    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 by flamy; 10-08-2011 at 12:15 PM. Reason: extra info


  5. Location
    Sweden
    Posts
    1,418
    Also note that var works in C# inside methods, just not in class field declarations.
    My Open Source Projects: UdpKit, IronJS, Free Unity Assets
    Need unity consulting? Contact me over PM or Here


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


  7. Location
    Adelaide, Australia
    Posts
    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:  
    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 by rokstar234; 10-09-2011 at 05:35 AM.
    you can't spell Community without Unity

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •