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

Question Object Reference not set to an instance of an Object error

Discussion in 'Scripting' started by RapidKnight, Apr 6, 2023.

  1. RapidKnight

    RapidKnight

    Joined:
    Nov 22, 2020
    Posts:
    2
    I've checked other threads but they aren't helping, and my code seems to be fine.
    It says it finds an error on line 20 of my mouse movement script.
    This is my mousemovement script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseMovement : MonoBehaviour
    6. {
    7.     public float mouseSensitivity = 100f;
    8.     float xRotation = 0f;
    9.     float YRotation = 0f;
    10.     void Start()
    11.     {
    12.       //Locking the cursor to the middle of the screen and making it invisible
    13.       Cursor.lockState = CursorLockMode.Locked;
    14.     }
    15.     void Update()
    16.     {
    17.         if (InventorySystem.Instance.isOpen == false)
    18.         {
    19.        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    20.        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    21.      
    22.        //control rotation around x axis (Look up and down)
    23.        xRotation -= mouseY;
    24.        //we clamp the rotation so we cant Over-rotate (like in real life)
    25.        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    26.        //control rotation around y axis (Look up and down)
    27.        YRotation += mouseX;
    28.        //applying both rotations
    29.        transform.localRotation = Quaternion.Euler(xRotation, YRotation, 0f);
    30.        }
    31.     }
    32. }
    33.  
    and this is my inventory system script
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class InventorySystem : MonoBehaviour
    6. {
    7.    public static InventorySystem Instance { get; set; }
    8.     public bool isOpen;
    9.     public GameObject inventoryScreenUI;
    10.     public List<GameObject> slotList = new List<GameObject>();
    11.     public List<string> itemList = new List<string>();
    12.     private GameObject itemToAdd;
    13.     private GameObject whatSlotToEquip;
    14.     public bool isFull;
    15.  
    16.     void Start()
    17.     {
    18.         isOpen = false;
    19.         PopulateSlotList();
    20.     }
    21.     private void PopulateSlotList()
    22.     {
    23.         foreach (Transform child in inventoryScreenUI.transform)
    24.         {
    25.             if (child.CompareTag("Slot"))
    26.             {
    27.                 slotList.Add(child.gameObject);
    28.             }
    29.  
    30.         }
    31.     }
    32.     void Update()
    33.     {
    34.         if (Input.GetKeyDown(KeyCode.I) && !isOpen)
    35.         {
    36.             Debug.Log("i is pressed");
    37.             inventoryScreenUI.SetActive(true);
    38.             Cursor.lockState = CursorLockMode.None;
    39.             isOpen = true;
    40.  
    41.         }
    42.         else if (Input.GetKeyDown(KeyCode.I) && isOpen)
    43.         {
    44.             inventoryScreenUI.SetActive(false);
    45.             Cursor.lockState = CursorLockMode.Locked;
    46.             isOpen = false;
    47.         }
    48.     }
    49. }
    50.  
    If someone could help me debug this that would be great.
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    743
    My guess is that your inventory system instance is null.

    Code (CSharp):
    1. InventorySystem.Instance
    Where do you assign it? Maybe you need to do it in the Awake method of your InventorySystem script.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,960
  4. RapidKnight

    RapidKnight

    Joined:
    Nov 22, 2020
    Posts:
    2
    I already checked if it was null, and the same error occurred, because isOpen is a boolean and cannot be null.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,960
    You're right, it can't be.

    Therefore you haven't actually completed Step #1 and yet you think you have.

    The null object will be on the LEFT side of any dot (
    .
    ) operator.

    The computer says, "Your move."