Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Other Cursor NullreferenceException when there is NOTHING TO reference

Discussion in 'Scripting' started by JBGamemaker, Oct 11, 2022.

  1. JBGamemaker

    JBGamemaker

    Joined:
    Dec 9, 2018
    Posts:
    50
    I'm trying to make an FNaF fangame that has a 360 View of the Office, and where if you look at an interactable you can click and it will do its function.
    the problem is that for some unknown reason (I SPECULATE that it is because the cursor randomly disconnects from the monitor containing the game view) it keeps sending me an Nullreferenceexception at random moments.
    it SAYS That there is nothing used for calls to other functions. but the problem is, there is NOTHING to set a REFERENCE TO.

    I tried asking about this on discord, but while I understand that they might be bussy ATM, I was at my wit's end and so decided to ask for help here

    the code for the three scripts

    the script that calculates what is being looked at
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player_interactions_script : MonoBehaviour
    6. {
    7.     public player_look_script donations;
    8.  
    9.     public Camera player;
    10.  
    11.     private bool interactionToggle;
    12.  
    13.     private Ray line;
    14.  
    15.     [Range(0.5f, 2.5f)]
    16.     public float distance = 0.5f;
    17.  
    18.     public LayerMask devicesLayer;
    19.  
    20.     private device_manager donation_reciever;
    21.  
    22.     private void Awake()
    23.     {
    24.         player = donations.player;
    25.     }
    26.     // Update is called once per frame
    27.  
    28.     private void FixedUpdate()
    29.     {
    30.         sightphysics();
    31.     }
    32.  
    33.     void sightphysics()
    34.     {
    35.         line = new Ray(player.transform.position, player.transform.forward);
    36.         RaycastHit interactionHit;
    37.  
    38.         interactionToggle = Physics.Raycast(line, out interactionHit, distance, devicesLayer);
    39.  
    40.         if (interactionToggle)
    41.         {
    42.             if(interactionHit.collider.tag == "Light Button")
    43.             {
    44.                 donation_reciever.light_toggle();
    45.             }
    46.             else if(interactionHit.collider.tag == "Door Button")
    47.             {
    48.                 donation_reciever.door_toggle();
    49.             }
    50.        
    51.         }
    52.     }
    53.    
    54. }
    55.  

    the code meant for each individual 'device'

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class device_manager : MonoBehaviour
    6. {
    7.     public GameObject doorButton;
    8.  
    9.     public GameObject lightButton;
    10.  
    11.     private bool lightEnabled;
    12.  
    13.     private bool doorEnabled;
    14.  
    15.     private void Awake()
    16.     {
    17.         lightEnabled = false;
    18.  
    19.         doorEnabled = false;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     public void light_toggle()
    24.     {
    25.         if (Input.GetMouseButton(0))
    26.         {
    27.             Debug.Log("Sucess");
    28.         }
    29.     }
    30.  
    31.     public void door_toggle()
    32.     {
    33.  
    34.     }
    35. }
    36.  

    I would REALLY love an Idea asto whats causing the NullReferenceException
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    There's no way to answer you. You don't say what/where the NullReferenceException is thrown. We cannot run the code in our heads to debug it for you.

    No idea what Cursor NullReferenceException means.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Don't start making assumptions. That is not on the list of the three things that fix a nullref.

    Those three things are ALWAYS the same. Start with the first one.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that

    You may wish to review your code. I see at least THREE DIFFERENT things that can be null (donations, player, donationsreceiver or whatever it was called), possibly more. Get to it! Nobody here can magically tell you.