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.