Problems with locking and hiding The Cursor!!

Hi
So my problem: it will not hide the cursor at the begin and if you press the SystemKey it wil have to show the cursor and panel. the panel is showing but the cursor is acting weard.
and if you click on resume it will hide the cursor and the panel.

but some how it’s not working did i do anything wrong?!

using UnityEngine;
using System.Collections;

public class SystemManager : MonoBehaviour {

    [Header("Player")]
    public Camera PlayerCam;

    [Header("SystemSelect")]
    public KeyCode SystemKey;
    public GameObject SSLP; //SystemSelectPanel

    [Header("Zoom")]
    public KeyCode Zoomin;
    public KeyCode Zoomout;
    public float AmountZoom;
    public float AmountZoomout;


    void Start ()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
        SSLP.SetActive (false);
    }
   

    void Update ()
    {
        //SystemPanel
        {
            if (Input.GetKeyDown (SystemKey))
                SSLP.SetActive (true);
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }

        //Zoom in
        {
            if (Input.GetKeyDown (Zoomin))
                PlayerCam.fieldOfView = AmountZoom;       
        }
        //Zoom OUT
        {
            if (Input.GetKeyDown (Zoomout))
                PlayerCam.fieldOfView = AmountZoomout;   
        }
    }

    public void Resume ()
    {
        SSLP.SetActive (false);

        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
}

Your code blocks are weird. The blocks should be immediately following the conditional statement, not wrapping around the whole thing.

//SystemPanel
{
    if (Input.GetKeyDown (SystemKey))
        SSLP.SetActive (true);
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
}

should be this

//SystemPanel
if (Input.GetKeyDown (SystemKey))
{
    SSLP.SetActive (true);
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
}

You can tell by how the indentations are handled how the logic is being handled- ie: everything that’s indented only runs if the conditional at the start of the indentation is true. There’s nothing illegal about grouping together blocks of code without a conditional the way that you did- it’s actually really useful to define a scope for a new variable with a common name like “x” or “y” so that there aren’t conflicts when repeating a similar action multiple times- that’s why there’s no error or warning. The only thing in your script that’s -only- being run when the SystemKey is pressed is SSLP.SetActive(true) - the Cursor is instead being set to active and unlocked every single frame, because it’s not in a block relying on the conditional’s value.

Hope that helps!

1 Like

hmm yeah i see i changed to what you recommended, and it’s working perfectly now so thank you so much!!!

hmm yeah i see i changed to what you recommended, and it’s working perfectly now so thank you so much!!!