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

Visible not working c# Unity 3d

Discussion in 'Scripting' started by BSimow, Dec 2, 2015.

  1. BSimow

    BSimow

    Joined:
    Sep 1, 2015
    Posts:
    6
    Hi

    I am new to unity 3D and scripting/coding,I need help!
    I am trying to make a fps (first person shooter) game and I want to hide my cursor but .visible is not working
    Here is my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Cursor : MonoBehaviour {
    5.     void Start ()
    6.     {
    7.         Cursor.lockState = CursorLockMode.Locked;
    8.         Cursor.visible = (false);
    9.     }
    10.     void Update ()
    11.     {
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.         Cursor.visible = (false);
    14.     }
    visible is marked as red in the script and I don't know why anybody that knows what is the problem please tell me.

    Thank you
    BSimow
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    Are you trying to do it within the editor (ie the play button)?
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Having a class called the same as a build in class in not such a smart idea.
    How should unity / monodevelop know if you man your class Cursor or the builtin class.
    Rename your class to something else and it should work fine.
     
    flonch likes this.
  4. BSimow

    BSimow

    Joined:
    Sep 1, 2015
    Posts:
    6
    Oh my god I am an idiot thanks for telling me.

    And yes I am trying to do it within the editor.
    Edit: I tried it right now but it still isn't working, here is my new script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Cursordelete : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.            void Start ()
    8.     {
    9.                 Cursor.lockState = CursorLockMode.Locked;
    10.                 Cursor.visible = (false);
    11.     }
    12.             void Update ()
    13.          
    14.  
    15.     {
    16.                 Cursor.lockState = CursorLockMode.Locked;
    17.                 Cursor.visible = (false);
    18.    }
    19. }
     
    Last edited: Dec 2, 2015
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    For one, when you renamed your class, did you rename the script file to be the same as well? And have you checked to make sure that the reference on the component to the script is not broken? (which can happen when you rename them)
     
  6. BSimow

    BSimow

    Joined:
    Sep 1, 2015
    Posts:
    6
    I made a new script,wrote all of it again and deleted the old one.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    Try building your project and running it outside of the editor. If I recall correctly the editor has a tendency to force it visible.
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    We also found that sometimes you have to click the mouse button in the scene (when running it from the editor & not a build) to sort of switch it from being able to be used by the editor to it being "in the game".
     
  9. BSimow

    BSimow

    Joined:
    Sep 1, 2015
    Posts:
    6
    Done but still there is the cursor
     
  10. BSimow

    BSimow

    Joined:
    Sep 1, 2015
    Posts:
    6
    tried to do it but still nothing
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Maybe try setting the visibility before setting the lock state?
     
  12. BSimow

    BSimow

    Joined:
    Sep 1, 2015
    Posts:
    6
    I deleted the project i just raged quit because there was another script the didnt want to work
    P.s thanks for the help