Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unassigned Reference Exception MouseLook and movement issue

Discussion in 'Scripting' started by rievvius, Jun 30, 2020.

  1. rievvius

    rievvius

    Joined:
    Jun 2, 2020
    Posts:
    2
    Hi,
    I'm new to Unity which is why I'm asking but Unity is giving me the following error: UnassignedReferenceException: The variable lookRoot of MouseLook has not been assigned. You probably need to assign the lookRoot variable of the MouseLook script in the inspector. UnityEngine.Transform.set_localRotation (UnityEngine.Quaternion value) <0x30053f70 + 0x0005a> in :0 MouseLook.LookAround () (at Assets/SampleScenes/Scripts/Player Scripts/MouseLook.cs:95) MouseLook.Update () (at Assets/SampleScenes/Scripts/Player Scripts/MouseLook.cs:56)

    I'm guessing that's why my player is also rotating a lot/moving a lot but I'm not sure.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour {
    6.  
    7.     [SerializeField]
    8.     private Transform playerRoot, lookRoot;
    9.  
    10.     [SerializeField]
    11.     private bool invert;
    12.  
    13.     [SerializeField]
    14.     private bool can_Unlock = true;
    15.  
    16.     [SerializeField]
    17.     private float sensivity = 5f;
    18.  
    19.     [SerializeField]
    20.     private int smooth_Steps = 10;
    21.  
    22.     [SerializeField]
    23.     private float smooth_Weight = 0.4f;
    24.  
    25.     [SerializeField]
    26.     private float roll_Angle = 10f;
    27.  
    28.     [SerializeField]
    29.     private float roll_Speed = 3f;
    30.  
    31.     [SerializeField]
    32.     private Vector2 default_Look_Limits = new Vector2(-70f, 80f);
    33.  
    34.     private Vector2 look_Angles;
    35.  
    36.     private Vector2 current_Mouse_Look;
    37.     private Vector2 smooth_Move;
    38.  
    39.     private float current_Roll_Angle;
    40.  
    41.     private int last_Look_Frame;
    42.  
    43.     // Use this for initialization
    44.     void Start () {
    45.  
    46.         Cursor.lockState = CursorLockMode.Locked;
    47.  
    48.     }
    49.    
    50.     // Update is called once per frame
    51.     void Update () {
    52.  
    53.         LockAndUnlockCursor();
    54.  
    55.         if(Cursor.lockState == CursorLockMode.Locked) {
    56.             LookAround();
    57.         }
    58.  
    59.     }
    60.  
    61.     void LockAndUnlockCursor() {
    62.  
    63.         if(Input.GetKeyDown(KeyCode.Escape)) {
    64.  
    65.             if(Cursor.lockState == CursorLockMode.Locked) {
    66.  
    67.                 Cursor.lockState = CursorLockMode.None;
    68.  
    69.  
    70.             } else {
    71.  
    72.                 Cursor.lockState = CursorLockMode.Locked;
    73.                 Cursor.visible = false;
    74.  
    75.             }
    76.  
    77.         }
    78.  
    79.     } // lock and unlock
    80.  
    81.     void LookAround() {
    82.  
    83.         current_Mouse_Look = new Vector2(
    84.             Input.GetAxis(MouseAxis.MOUSE_Y), Input.GetAxis(MouseAxis.MOUSE_X));
    85.  
    86.         look_Angles.x += current_Mouse_Look.x * sensivity * (invert ? 1f : -1f);
    87.         look_Angles.y += current_Mouse_Look.y * sensivity;
    88.  
    89.         look_Angles.x = Mathf.Clamp(look_Angles.x, default_Look_Limits.x, default_Look_Limits.y);
    90.  
    91.         //current_Roll_Angle =
    92.             //Mathf.Lerp(current_Roll_Angle, Input.GetAxisRaw(MouseAxis.MOUSE_X)
    93.                        //* roll_Angle, Time.deltaTime * roll_Speed);
    94.  
    95.         lookRoot.localRotation = Quaternion.Euler(look_Angles.x, 0f, 0f);
    96.         playerRoot.localRotation = Quaternion.Euler(0f, look_Angles.y, 0f);
    97.  
    98.  
    99.     } // look around
    100.  
    101.  
    102. } // class
    103.  
    104.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Welcome! Definitely check out lots of tutorials.

    The basic premise / pattern with variables marked either public or SerializeField is that they are usually intended to have something else dragged into them in the inspector.

    This is not always the case, but it's the "basic Unity way of doing things," and you should get familiar with seeing it, and seeing "Hey, unassigned reference, I must have forgotten to drag something into a slot."

    In this case, something in your MouseLook script needs to be assigned. Based on the name, I'm gong to guess the Camera is supposed to be dragged into this slot.

    Fortunately the error message will (usually) lead you right to the variable, and you can then decide what "thing" in the editor window should be dragged into that variable in the inspector.

    Also, FYI, Null Reference Error is virtually identical to Unassigned Reference Error, and here are some notes on how to fix a NullReferenceException in Unity3D:

    http://plbm.com/?p=221
     
  3. rievvius

    rievvius

    Joined:
    Jun 2, 2020
    Posts:
    2
    Hi, thank you, you were right! I accidentally put it on "Player" instead of "Look Root" therefore it was not assigned.

    However I still have a movement issue where my player keeps rotating or moving around like crazy. Do you have any idea if that has to do with my MouseLook script?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    One easy way to find out: disable the mouselook script and see if it still happens.