- Home /
 
Get a float from a different GameObject? (MouseLook)
I need to access a public float from the fps controller's mouselook script. I think it is on the player? It is accessed from FirstPersonController script like it is in a drop down in the inspector. It is [Serializable]. Why doesnt this work? (im a noob and need help making a sensitivity slider and I cannot find it anywhere, if anyone has a script for that (FPSController) I would love to have it. How do i access the float? (My scirpt):
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class sensitivitySliderScript : MonoBehaviour {
     [SerializeField]
     public MouseLook m_MouseLook;
     public float xfloat;
 
 
 
     private void Start()
     {
         xfloat = MouseLook.XSensitivity;
     }
 
 
     void Update () {
 
     
 
        // PlayerPrefs.SetFloat("currentX", MouseLook.XSensitivity);
 
 
      //   MouseLook.XSensitivity = PlayerPrefs.GetFloat("currentX", 0);
 
 
 
     }
 }
 
 
               MouseLook floats I need: using System; using UnityEngine; using UnityEngine.UI; using UnityStandardAssets.CrossPlatformInput;
 namespace UnityStandardAssets.Characters.FirstPerson
 {
     [Serializable]
     public class MouseLook
     {
    
         public float XSensitivity = 2;
         public float YSensitivity = 2;
 
              Answer by eskivor · Jun 15, 2017 at 07:26 AM
There are 2 solutions for you :
either you have to replace your line xfloat = MouseLook.XSensitivity; by xfloat = m_MouseLook.XSensitivity;
or
your XSensitivity variable has to be static, like this : public static float XSensitivity = 2; then your line xfloat = MouseLook.XSensitivity; will be ok and the line public MouseLook m_MouseLook; will be useless because XSensitivity will be accessible from all scripts.
Your answer