Search code examples
unity-game-enginecursortoggle

cursor is visible not matter what my code says


hey so originally i made this pause menu script and it make my cursor visible but it doesn't turn the cursor off i even made the script on/off seperate codes at one point but it still dosnt turn the cursor off. any one have any idea why? thanks. and i haven't written enough so here are some words to read so i hope you enjoy them i put no effort into these here words

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PMScript : MonoBehaviour
    {
    public GameObject VolumeSettings;
    public GameObject mainSettings;
    public GameObject GraphicsSettings;
    public GameObject HitMarker_;
    public GameObject pmui;


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

    // Update is called once per frame
    void Update()
    {
        if (VolumeSettings == true)
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }

        if (mainSettings == true)
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }

        if (GraphicsSettings == true)
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            HitMarker_.SetActive(false);
            pmui.SetActive(true);
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }

        if (VolumeSettings == false)
        {
            if (mainSettings == false)
            {
                if (GraphicsSettings == false)
                {
                    if (pmui == false)
                    {
                        Cursor.visible = false;
                        Cursor.lockState = CursorLockMode.Locked;
                    }
                }
            }
        }
    }
}

Solution

  • When you compare a GameObject with a bool such as

    VolumeSettings == true
    

    What you're checking is whether or not VolumeSettings is assigned in the inspector.

    If this is intentional make sure that VolumeSettings(along with the others) is not assigned if you want the cursor to be locked.

    If you're instead trying to check whether the GameObject is active or not consider using the following

    if(VolumeSettings.activeSelf == true)