Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Can't find variable in inspector. Any reason why this might've happened?

Discussion in 'Editor & General Support' started by arkdoggo, Oct 29, 2023.

  1. arkdoggo

    arkdoggo

    Joined:
    Oct 28, 2023
    Posts:
    3
    I was trying to make a slider element in my settings menu to allow the player to change the mouse sensitivity on a range of 0.1 to 7, but when I added a Slider variable, I couldn't see it in the inspector, (Image below) which meant I couldn't properly set it's value.

    upload_2023-10-29_19-3-44.png

    Here's a bit of my code to further explain what I mean:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6.  
    7. public class FPSController : MonoBehaviour
    8. {
    9.  
    10.     [Range(0.1f, 7f)]
    11.     public float lookSpeed = 2f;
    12.     private readonly Slider lookSpeedSlider;
    13.     //other variables
    14.  
    15.     void Update() {
    16.         lookSpeed = lookSpeedSlider.value;
    17.         //more irrelevant code
    18.     }
    19. }
    Any help is appreciated, whether it's a solution, further explenations/instructions on what to do or a work-around.

    thanks in advance.
     
  2. Bigpete591

    Bigpete591

    Joined:
    May 7, 2013
    Posts:
    60
    Try making the field public instead of private.
     
    arkdoggo likes this.
  3. arkdoggo

    arkdoggo

    Joined:
    Oct 28, 2023
    Posts:
    3
    Tried that, same result sadly.

    Edit: I also added a [SerializeField] and removed the readonly attribute but it still didn't work.
     
    Last edited: Oct 29, 2023
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,240
    Your code above presumably is trying to use
    UnityEngine.UIElements.Slider
    ...

    Are you perhaps looking for
    UnityEngine.UI.Slider
    ?
     
    arkdoggo and spiney199 like this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,187
    On top of the above, Unity also can't serialise readonly fields.
     
    arkdoggo and Kurt-Dekker like this.
  6. arkdoggo

    arkdoggo

    Joined:
    Oct 28, 2023
    Posts:
    3
    Thanks, it worked!