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 am getting errors from the script I am trying to use!

Discussion in 'Scripting' started by deagles4life, Jun 3, 2015.

  1. deagles4life

    deagles4life

    Joined:
    May 15, 2015
    Posts:
    29
    I don't know whats wrong with it (I don't know anything about scripting) so can someone help me please because i get 3 errors like this
    error CS0116: A namespace can only contain types and namespace declarations
    Code (CSharp):
    1. bool CursorLockedVar;
    2.  
    3. void Start ()
    4. {
    5.     Cursor.lockState = CursorLockMode.Locked;
    6.     Cursor.visible = (false);
    7.     CursorLockedVar = (true);
    8. }
    9.  
    10. void Update ()
    11. {
    12.     if (Input.GetKeyDown ("escape") && !CursorLockedVar) {
    13.         Cursor.lockState = CursorLockMode.Locked;
    14.         Cursor.visible = (false);
    15.         CursorLockedVar = (true);
    16.     }
    17.     else if(Input.GetKeyDown("escape") && CursorLockedVar){
    18.         Cursor.lockState = CursorLockMode.None;
    19.         Cursor.visible = (true);
    20.         CursorLockedVar = (false);
    21.     }
    22. }
     
  2. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Is this the entire script file? I don't see any class definition around it if so.
     
  3. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
    To add to what CaptainMurphy wrote, add this to the top of the script, before your class definition as well:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    This stuff is done for you if you create a new script file from within the editor. Then you just need to add your own code to the correct places. I strongly recommend doing it this way rather than creating your scripts from empty text files, particularly if you're new to it.
     
  4. deagles4life

    deagles4life

    Joined:
    May 15, 2015
    Posts:
    29
    I don't know what to do but you said something about using the default start thing so i did and I still get the error
    Code (CSharp):
    1. // <auto-generated>
    2. //     This code was generated by a tool.
    3. //     Runtime Version:4.0.30319.34014
    4. //
    5. //     Changes to this file may cause incorrect behavior and will be lost if
    6. //     the code is regenerated.
    7. // </auto-generated>
    8. //------------------------------------------------------------------------------
    9. using System;
    10. namespace AssemblyCSharp
    11. {
    12.     public class hideandlockcurser
    13.     {
    14.         public hideandlockcurser ()
    15.         {
    16.         }
    17.     }
    18. }
    19. bool CursorLockedVar;
    20.  
    21. void Start ()
    22. {
    23.     Cursor.lockState = CursorLockMode.Locked;
    24.     Cursor.visible = (false);
    25.     CursorLockedVar = (true);
    26. }
    27.  
    28. void Update ()
    29. {
    30.     if (Input.GetKeyDown ("escape") && !CursorLockedVar) {
    31.         Cursor.lockState = CursorLockMode.Locked;
    32.         Cursor.visible = (false);
    33.         CursorLockedVar = (true);
    34.     }
    35.     else if(Input.GetKeyDown("escape") && CursorLockedVar){
    36.         Cursor.lockState = CursorLockMode.None;
    37.         Cursor.visible = (true);
    38.         CursorLockedVar = (false);
    39.     }
    40. }
    41.  
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    open unity, open/create a project, r-click on the project tab, create > c# script (name script), open script (double click on it)

    the script will have

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DefaultScript : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.     }
    16. }
    17.  
    this should be the starting point of every script you make for unity until you know more about what you are doing
     
    CaptainMurphy likes this.
  6. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Your script should look like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScriptName : MonoBehaviour {
    5.  
    6.    bool CursorLockedVar;
    7.    void Start ()
    8.    {
    9.        Cursor.lockState = CursorLockMode.Locked;
    10.        Cursor.visible = (false);
    11.        CursorLockedVar = (true);
    12.    }
    13.  
    14.    void Update ()
    15.    {
    16.        if (Input.GetKeyDown ("escape") && !CursorLockedVar) {
    17.            Cursor.lockState = CursorLockMode.Locked;
    18.            Cursor.visible = (false);
    19.            CursorLockedVar = (true);
    20.        }
    21.        else if(Input.GetKeyDown("escape") && CursorLockedVar){
    22.            Cursor.lockState = CursorLockMode.None;
    23.            Cursor.visible = (true);
    24.            CursorLockedVar = (false);
    25.        }
    26.    }
    27. }
     
  7. deagles4life

    deagles4life

    Joined:
    May 15, 2015
    Posts:
    29
    K so this should work? because I can't check untill I am at home because I am in school right now