Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Unity throws UnassignedReferenceException on a variable that is definitely assigned. Code works just fine too.

$
0
0
My error tells me that my camera variable is unassigned. It is a public variable that I assign from the inspector. I get the following error: ---------- UnassignedReferenceException: The variable normalCam of motion has not been assigned. You probably need to assign the normalCam variable of the motion script in the inspector. Com.neon.fps.motion.FixedUpdate () (at Assets/scripts/motion.cs:45) ---------- This confuses me because I cave used the inspector to assign the camera... and the code runs just fine with the expected result. Should I simply ignore the error? Here is my code in case that is helpful: will comment //ERROR HERE where the error is please IGNORE the rest of my comments. Those are notes to self that I have figured out. using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Com.neon.fps { public class motion : MonoBehaviour { public float speed; //when something is public you can change it from the inspector in unity public float sprintMod; public Camera normalCam; private Rigidbody rig; private float baseFOV; private float sprintFOVMod = 1.25f; // Start is called before the first frame update void Start() { //Camera.main.enabled = false; rig = GetComponent(); baseFOV = normalCam.fieldOfView; } // Update is called once per frame void FixedUpdate() //what does fixed update do compared to just update? { float t_hmove = Input.GetAxisRaw("Horizontal"); float t_vmove = Input.GetAxisRaw("Vertical"); bool sprint = Input.GetKey(KeyCode.LeftShift); //could also add || right shift bool isSprinting = sprint && t_vmove > 0; Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove); t_direction.Normalize(); float t_adjustedSpeed = speed; if (isSprinting) t_adjustedSpeed *= sprintMod; //Debug.Log("error"); rig.velocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime; //apparently time.deltatime needs to be here or it defeats the purpose of using fixed update... why? if(isSprinting) { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVMod, Time.deltaTime * 8f); } else { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f); } //ERROR HERE } } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>