- Home /
MissingReferenceException:,MissingReferenceException
I am making a multiplayer FPS using Photon, but I always get a "MissingReferenceException: The object of type 'Camera' has been destroyed but you are still trying to access it." error when I try to shoot, I think it's on line 59, but when I delate it the camera just breaks.
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Hashtable = ExitGames.Client.Photon.Hashtable;
public class PlayerController : MonoBehaviourPunCallbacks, IDamageable
{
[SerializeField] Image healthbarImage;
[SerializeField] GameObject ui;
[SerializeField] GameObject cameraHolder;
public Text ammo2;
[SerializeField] float mouseSensitivity, sprintSpeed, walkSpeed, jumpForce, smoothTime;
public Text reloadtext;
[SerializeField] Item[] items;
Vector2 rotation;
public int itemIndex;
int previousItemIndex = -1;
float verticalLookRotation;
bool grounded;
Vector3 smoothMoveVelocity;
Vector3 moveAmount;
public float a;
Rigidbody rb;
PhotonView PV;
const float maxHealth = 100f;
float currentHealth = maxHealth;
PlayerManager playerManager;
public void Awake()
{
rb = GetComponent<Rigidbody>();
PV = GetComponent<PhotonView>();
Cursor.lockState = CursorLockMode.Locked;
playerManager = PhotonView.Find((int)PV.InstantiationData[0]).GetComponent<PlayerManager>();
Vector2 rotation = Vector2.zero;
}
public void Start()
{
DontDestroyOnLoad(cameraHolder);
if (PV.IsMine)
{
EquipItem(0);
}
else
{
Destroy(GetComponentInChildren<Camera>().gameObject);
Destroy(rb);
Destroy(ui);
}
Check();
StartCoroutine(A());
}
public void Check()
{
if (itemIndex == 0)
{
ammo2.text = "30";
}
if(itemIndex == 1)
{
ammo2.text = "10";
}
}
public void Update()
{
if (!PV.IsMine)
return;
if(Input.GetMouseButtonDown(0) && itemIndex == 1)
{
items[itemIndex].Use();
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
Cursor.lockState = CursorLockMode.None;
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.Locked;
}
if (itemIndex == 0)
{
a = 0.1f;
ammo2.text = "30";
}
if (itemIndex == 1)
{
ammo2.text = "10";
}
Look();
Move();
Jump();
for (int i = 0; i < items.Length; i++)
{
if (Input.GetKeyDown((i + 1).ToString()))
{
EquipItem(i);
break;
}
}
if (Input.GetAxisRaw("Mouse ScrollWheel") > 0f)
{
if (itemIndex >= items.Length - 1)
{
EquipItem(0);
}
else
{
EquipItem(itemIndex + 1);
}
}
else if (Input.GetAxisRaw("Mouse ScrollWheel") < 0f)
{
if (itemIndex <= 0)
{
EquipItem(items.Length - 1);
}
else
{
EquipItem(itemIndex - 1);
}
}
if (transform.position.y < -10f)
{
Die();
}
}
IEnumerator A()
{
Something();
yield return new WaitForSeconds(a);
StartCoroutine(A());
}
public void Something ( )
{
if (Input.GetMouseButton(0))
{
items[itemIndex].Use();
}
}
void Look()
{
rotation.y += Input.GetAxis("Mouse X");
rotation.x += -Input.GetAxis("Mouse Y");
transform.eulerAngles = (Vector2)rotation * mouseSensitivity;
}
void Move()
{
Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
moveAmount = Vector3.SmoothDamp(moveAmount, moveDir * (Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : walkSpeed), ref smoothMoveVelocity, smoothTime);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
rb.AddForce(transform.up * jumpForce);
}
}
public void EquipItem(int _index)
{
if (_index == previousItemIndex)
return;
itemIndex = _index;
items[itemIndex].itemGameObject.SetActive(true);
if (previousItemIndex != -1)
{
items[previousItemIndex].itemGameObject.SetActive(false);
}
previousItemIndex = itemIndex;
if (PV.IsMine)
{
Hashtable hash = new Hashtable();
hash.Add("itemIndex", itemIndex);
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
}
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
{
if (!PV.IsMine && targetPlayer == PV.Owner)
{
EquipItem((int)changedProps["itemIndex"]);
}
}
public void SetGroundedState(bool _grounded)
{
grounded = _grounded;
}
void FixedUpdate()
{
if (!PV.IsMine)
return;
rb.MovePosition(rb.position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
}
public void TakeDamage(float damage)
{
PV.RPC("RPC_TakeDamage", RpcTarget.All, damage);
}
[PunRPC]
void RPC_TakeDamage(float damage)
{
if (!PV.IsMine)
return;
currentHealth -= damage;
healthbarImage.fillAmount = currentHealth / maxHealth;
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
playerManager.Die();
}
}
Answer by Monsoonexe · May 21, 2021 at 06:09 PM
MissingReferenceExceptions are called when an Object reference exists, is destroyed, and then is later referenced again. Unity can tell that the reference used to exist but no longer does.
As a note, Destroy()
does not destroy immediately. Rather, it marks something be destroyed at the end of the frame. It's possible that more than one class is invoking the Destroy() method.
Answer by Steel_Pact · May 24, 2021 at 09:19 PM
I just tried again and there wasn't a error, than the next time I opened Unity there was but then after 2 hours or so it went away, I didn't change anything. Is this a bug?
Your answer
![](https://koobas.hobune.stream/wayback/20220613034008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
MissingReferenceException Help 1 Answer
DontDestroyOnLoad error 2 Answers
MissingReferenceException Help Please! 0 Answers
Why Player input aways get MissingReferenceException? 0 Answers