質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

3646閲覧

Unity Photon2 MissingReferenceExceptionというエラーが出る

creatorsGame

総合スコア18

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/04/29 07:05

編集2020/04/30 14:03
エラー MissingReferenceException: The object of type 'PhotonView' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Photon.Pun.PhotonNetwork.LocalCleanupAnythingInstantiated (System.Boolean destroyInstantiatedGameObjects) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:269) Photon.Pun.PhotonNetwork.LeftRoomCleanup () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:245) Photon.Pun.PhotonNetwork+<>c.<.cctor>b__124_0 (Photon.Realtime.ClientState previousState, Photon.Realtime.ClientState state) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetwork.cs:1006) Photon.Realtime.LoadBalancingClient.set_State (Photon.Realtime.ClientState value) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:383) Photon.Realtime.LoadBalancingClient.Disconnect (Photon.Realtime.DisconnectCause cause) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:1067) Photon.Realtime.ConnectionHandler.OnDisable () (at Assets/Photon/PhotonRealtime/Code/ConnectionHandler.cs:91) Photon.Pun.PhotonHandler.OnDisable () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:132)
注意マーク PUN is in development mode (development build). As the 'dev region' is not empty (jp) it overrides the found best region. See PhotonServerSettings. UnityEngine.Debug:LogWarning(Object) Photon.Pun.PhotonNetwork:OnRegionsPinged(RegionHandler) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2312) Photon.Realtime.RegionHandler:OnPreferredRegionPinged(Region) (at Assets/Photon/PhotonRealtime/Code/RegionHandler.cs:251) Photon.Realtime.RegionPinger:RegionPingThreaded() (at Assets/Photon/PhotonRealtime/Code/RegionHandler.cs:459) ExitGames.Client.Photon.<>c__DisplayClass6_0:<StartBackgroundCalls>b__0() System.Threading.ThreadHelper:ThreadStart()

上記のエラーと注意マークが出て困っています。Photon2関係のスクリプトはいじっていないのでエラーで示されているスクリプトは関係がないと自分は思っているのですが、載せないと分からない場合はご指摘お願いいたします。
また、実際に起きている問題としては以下のように片方のプレイヤーが落ちたり、元の位置に戻ったりが繰り返されます。

イメージ説明

原因のプログラム

C#

1'Player' 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using Photon.Pun; 6 7public class Player : MonoBehaviourPunCallbacks 8{ 9 public float walkSpeed = 3f; 10 public float runSpeed = 6f; 11 private CharacterController Controller; 12 private Vector3 velocity; 13 private Animator animator; 14 public Camera normalCam; 15 public GameObject CameraParent; 16 public float mouseSensitivity = 100f; 17 public Transform playerBody; 18 private float xRotation = 0f; 19 private bool runFlag = false; 20 [SerializeField] 21 private Transform spine; 22 private void Start() 23 { 24 animator = GetComponent<Animator>(); 25 Controller = GetComponent<CharacterController>(); 26 CameraParent.SetActive(photonView.IsMine); 27 } 28 29 private void Update() 30 { 31 if (!photonView.IsMine) return; 32 if (Controller.isGrounded) 33 { 34 velocity = Vector3.zero; 35 var horizontal = Input.GetAxis("Horizontal"); 36 var vertical = Input.GetAxis("Vertical"); 37 velocity = (transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal")).normalized; 38 39 float speed = 0f; 40 if (Input.GetButton("Run") && Input.GetKey(KeyCode.W) && (!Input.GetButton("Fire2"))) 41 { 42 runFlag = true; 43 speed = runSpeed; 44 } 45 else 46 { 47 runFlag = false; 48 speed = walkSpeed; 49 } 50 velocity *= speed; 51 if (velocity.magnitude > 0) 52 { 53 animator.SetFloat("MoveX", horizontal); 54 animator.SetFloat("MoveY", vertical); 55 if (runFlag && Input.GetKey(KeyCode.W) && (!Input.GetButton("Fire2"))) { animator.SetFloat("Speed", 2.1f); } 56 else 57 { 58 animator.SetFloat("Speed", 0f); 59 } 60 } 61 else 62 { 63 animator.SetFloat("Speed", 0f); 64 animator.SetFloat("MoveX", 0f); 65 animator.SetFloat("MoveY", 0f); 66 } 67 } 68 velocity.y += Physics.gravity.y * Time.deltaTime; 69 Controller.Move(velocity * Time.deltaTime); 70 } 71 private void LateUpdate() 72 { 73 if (!photonView.IsMine) return; 74 float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; 75 float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; 76 xRotation -= mouseY; 77 xRotation = Mathf.Clamp(xRotation, -50, 40); 78 playerBody.Rotate(Vector3.up * mouseX); 79 spine.rotation = Quaternion.Euler(spine.eulerAngles.x + xRotation, spine.eulerAngles.y, spine.eulerAngles.z); 80 spine.localEulerAngles = new Vector3(xRotation, 0, 0); 81 normalCam.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); 82 } 83} 84 85

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fiveHundred

2020/04/29 13:29

破棄されたゲームオブジェクトを格納した変数を参照している際に出るエラーです。 どこでエラーが発生しているかが書かれていないため、どこを変更すればいいのかは分かりません。
guest

回答2

0

自己解決

CharacterControllerではなくRigidbodyを使ってプレイヤーを動かすスクリプトに変更したら解決しました。

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6namespace Test 7{ 8 9 public class Player : MonoBehaviourPunCallbacks 10 { 11 public float speed; 12 public float runModifier; 13 public Camera normalCam; 14 public GameObject CameraParent; 15 public float jumpForce; 16 public Transform groundDetector; 17 public LayerMask ground; 18 private Animator animator; 19 private Rigidbody rig; 20 //camera 21 public float mouseSensitivity = 100f; 22 public Transform playerBody; 23 float xRotation = 0f; 24 [SerializeField] 25 private Transform spine; 26 27 private void Start() 28 { 29 rig = GetComponent<Rigidbody>(); 30 animator = GetComponent<Animator>(); 31 Cursor.visible = false; 32 Cursor.lockState = CursorLockMode.Locked; 33 CameraParent.SetActive(photonView.IsMine); 34 if (!photonView.IsMine) 35 { 36 gameObject.layer = 9; 37 } 38 } 39 private void FixedUpdate() 40 { 41 if (!photonView.IsMine) return; 42 if (Input.GetKeyDown(KeyCode.Escape)) 43 { 44 Cursor.visible = true; 45 Cursor.lockState = CursorLockMode.None; 46 } 47 //Axies 48 float move_h = Input.GetAxisRaw("Horizontal"); 49 float move_v = Input.GetAxisRaw("Vertical"); 50 51 //Controls 52 bool run = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); 53 bool jump = Input.GetKeyDown(KeyCode.Space); 54 55 //States 56 bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.01f, ground); 57 bool isJumping = jump && isGrounded; 58 bool isRunning = run && move_v > 0;// && isGrounded && !jump; 59 60 //Jumping 61 if (isJumping) 62 { 63 rig.AddForce(Vector3.up * jumpForce); 64 } 65 66 //Movement 67 Vector3 direction = new Vector3(move_h, 0, move_v); 68 direction.Normalize(); 69 70 float adjustedSpeed = speed; 71 if (isRunning) adjustedSpeed *= runModifier; 72 73 Vector3 targetVelocity = transform.TransformDirection(direction) * adjustedSpeed * Time.deltaTime; 74 targetVelocity.y = rig.velocity.y; 75 rig.velocity = targetVelocity; 76 77 //animation 78 if (direction.magnitude > 0f) 79 { 80 animator.SetFloat("MoveX", move_h); 81 animator.SetFloat("MoveY", move_v); 82 if (isRunning) 83 { 84 animator.SetFloat("Speed", 2.1f); 85 } 86 else 87 { 88 animator.SetFloat("Speed", 0f); 89 } 90 } 91 else 92 { 93 animator.SetFloat("MoveX", 0f); 94 animator.SetFloat("MoveY", 0f); 95 animator.SetFloat("Speed", 0f); 96 } 97 } 98 private void LateUpdate() 99 { 100 if (!photonView.IsMine) return; 101 float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; 102 float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; 103 xRotation -= mouseY; 104 xRotation = Mathf.Clamp(xRotation, -50, 40); 105 playerBody.Rotate(Vector3.up * mouseX); 106 spine.rotation = Quaternion.Euler(spine.eulerAngles.x + xRotation, spine.eulerAngles.y, spine.eulerAngles.z); 107 spine.localEulerAngles = new Vector3(xRotation, 0, 0); 108 normalCam.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); 109 } 110 111 } 112 113}

投稿2020/05/03 02:59

creatorsGame

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

エスパー回答ですが、Photonで共有しているゲームオブジェクトを削除する時にPhotonNetwork.Destroy()ではなく普通のDestroy()を使っているのが原因ではないかなと思います。

投稿2020/04/30 07:08

fiveHundred

総合スコア9796

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

creatorsGame

2020/04/30 14:15 編集

ご回答ありがとうございます。 後から分かった事として上記の'原因のプログラム'をオフにすることですべてのエラー、またはすり抜けるバグなどが直りました。ただ、このコードのどこが原因かがわかっておらず、このプログラムはプレイヤーを動かすのに必要なので、どこをどう変更すれば良いでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問