Search Unity

Code still works as intended despite MissingReferenceException

Discussion in 'Scripting' started by Clydey2Times, Jul 8, 2020.

  1. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Hey all. I'm enabling and disabling various canvases via a raycast. It works (or it appears to) as intended, but the console is being spammed with a MissingReferenceException for the canvas variable. I have no idea why. If it no longer exists (as the error suggests) how is it enabling and disabling the component?

    The full error message is as follows:

    "MissingReferenceException: The variable pickupMessage of AmmoPickup doesn't exist anymore.
    You probably need to reassign the pickupMessage variable of the 'AmmoPickup' script in the inspector.
    AmmoPickup.Update () (at Assets/Scripts/AmmoPickup.cs:58)"


    Here's the script.

    Code (CSharp):
    1. public class AmmoPickup : MonoBehaviour
    2. {
    3.    
    4.  
    5.     [SerializeField] float distance = 4.0f;
    6.     [SerializeField] Canvas pickupMessage;
    7.     [SerializeField] Canvas gunReticuleCanvas;
    8.     [SerializeField] private bool canSeePickup = false;
    9.  
    10.     private RaycastHit hit;
    11.     private float rayDistance;
    12.     private Ammo ammo;
    13.     private AmmoDetails ammoDetails;
    14.  
    15.     private void Start()
    16.     {
    17.      
    18.         pickupMessage.enabled = false;
    19.         gunReticuleCanvas.enabled = true;
    20.        
    21.     }
    22.  
    23.     private void Update()
    24.     {
    25.         if (Physics.Raycast(transform.position, transform.forward, out hit, rayDistance))
    26.         {
    27.             if(hit.transform.tag == "Ammo")
    28.             {
    29.                 canSeePickup = true;
    30.  
    31.                 if (Input.GetKeyDown(KeyCode.Return))
    32.                 {
    33.                     ammoDetails = hit.transform.GetComponent<AmmoDetails>();
    34.                     ammo = FindObjectOfType<Ammo>();
    35.                     ammo.IncreaseCurrentAmmo(ammoDetails.ammoType, ammoDetails.ammoAmount);
    36.                     Destroy(hit.transform.gameObject);
    37.                 }
    38.             }
    39.             else
    40.             {
    41.                 canSeePickup = false;
    42.             }
    43.         }
    44.  
    45.         if(canSeePickup == true)
    46.         {
    47.             pickupMessage.enabled = true;
    48.        
    49.             rayDistance = 2000f;
    50.             gunReticuleCanvas.enabled = false;
    51.         }
    52.         if(canSeePickup == false)
    53.         {
    54.             if (pickupMessage.enabled == true)
    55.             {
    56.                 pickupMessage.enabled = false;
    57.             }
    58.             rayDistance = distance;
    59.             gunReticuleCanvas.enabled = true;
    60.         }
    61.     }
    62.  
    63.    
    64. }
    Any ideas? As I said, the code seems to work. The error is going to drive me crazy unless I get to the bottom of it, though.
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    Sound like you have the script attached somewhere else where it's not configured correctly?

    Add a
    Debug.Log("MESSAGE", this);
    call right before where the error happens, reproduce the error and pause the game. Then you can click the message and Unity will highlight the object that caused the message in the hierarchy. This should let you check which object the message is coming from and check the canvas variable in the inspector.
     
    PraetorBlue likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    The error message you posted doesn't really line up with the code you posted so I assume you changed something after copying the message.

    One possibility is that you have more copies of this script in your Scene than you think. One of them is failing and one is successful.
     
  4. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Damn, you're right. I totally forgot that I'd moved the script onto a new object and forgot to remove it from the other one.

    Thanks for figuring it out.
     
    PraetorBlue likes this.
  5. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    I accidentally attached the script on two different objects and references were missing on one of them. Thanks for replying.