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();
}
}