Code still works as intended despite MissingReferenceException

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.

public class AmmoPickup : MonoBehaviour
{
   

    [SerializeField] float distance = 4.0f;
    [SerializeField] Canvas pickupMessage;
    [SerializeField] Canvas gunReticuleCanvas;
    [SerializeField] private bool canSeePickup = false;

    private RaycastHit hit;
    private float rayDistance; 
    private Ammo ammo;
    private AmmoDetails ammoDetails;

    private void Start()
    {
     
        pickupMessage.enabled = false;
        gunReticuleCanvas.enabled = true;
       
    }

    private void Update()
    {
        if (Physics.Raycast(transform.position, transform.forward, out hit, rayDistance))
        {
            if(hit.transform.tag == "Ammo")
            {
                canSeePickup = true;

                if (Input.GetKeyDown(KeyCode.Return))
                {
                    ammoDetails = hit.transform.GetComponent<AmmoDetails>();
                    ammo = FindObjectOfType<Ammo>();
                    ammo.IncreaseCurrentAmmo(ammoDetails.ammoType, ammoDetails.ammoAmount);
                    Destroy(hit.transform.gameObject);
                }
            }
            else
            {
                canSeePickup = false;
            }
        }

        if(canSeePickup == true)
        {
            pickupMessage.enabled = true;
       
            rayDistance = 2000f;
            gunReticuleCanvas.enabled = false;
        }
        if(canSeePickup == false)
        {
            if (pickupMessage.enabled == true)
            {
                pickupMessage.enabled = false;
            }
            rayDistance = distance;
            gunReticuleCanvas.enabled = true;
        }
    }

   
}

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.

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.

1 Like

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.

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.

1 Like

I accidentally attached the script on two different objects and references were missing on one of them. Thanks for replying.