Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Steel_Pact · May 20, 2021 at 05:11 PM · missingreferenceexception

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();
     }
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MissingReferenceException Help 1 Answer

DontDestroyOnLoad error 2 Answers

MissingReferenceException Help Please! 0 Answers

Why Player input aways get MissingReferenceException? 0 Answers

Odd collision behaviour with If/while statement 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges