Search Unity

Third Party [HELP] Photon Pun 2 Hashtable Error

Discussion in 'Multiplayer' started by SpyderManToo, Jul 27, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    im following this series

    and am currently on the last part of this vid, i get this weird error when it try to build and run in both unity and the app
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. PlayerController.OnPlayerPropertiesUpdate (Photon.Realtime.Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps) (at Assets/Scripts/PlayerController.cs:143)
    3. Photon.Realtime.InRoomCallbacksContainer.OnPlayerPropertiesUpdate (Photon.Realtime.Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProp) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:4261)
    4. Photon.Realtime.LoadBalancingClient.ReadoutProperties (ExitGames.Client.Photon.Hashtable gameProperties, ExitGames.Client.Photon.Hashtable actorProperties, System.Int32 targetActorNr) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2238)
    5. Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3277)
    6. ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:898)
    7. ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:563)
    8. ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1863)
    9. Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:221)
    10. Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:147)
    11.  
    help, im not sure what it means

    heres my full script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Hashtable = ExitGames.Client.Photon.Hashtable;
    6. using Photon.Realtime;
    7.  
    8. public class PlayerController : MonoBehaviourPunCallbacks
    9. {
    10.     [SerializeField]
    11.     GameObject camHolder;
    12.  
    13.     [SerializeField]
    14.     float mouseSens, sprintSpeed, walkSpeed, jumpForce, smoothTime;
    15.  
    16.     [SerializeField]
    17.     Item[] items;
    18.  
    19.     int itemIndex;
    20.     int previousItemIndex = -1;
    21.  
    22.     float verticalLookRot;
    23.     bool grounded;
    24.     Vector3 smoothMoveVelocity;
    25.     Vector3 moveAmount;
    26.  
    27.     Rigidbody rb;
    28.  
    29.     PhotonView pv;
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.         rb = GetComponent<Rigidbody>();
    35.         pv = GetComponent<PhotonView>();
    36.  
    37.         if(pv.IsMine)
    38.         {
    39.             EquipItem(0);
    40.         }
    41.         else
    42.         {
    43.             Destroy(GetComponentInChildren<Camera>().gameObject);
    44.             Destroy(rb);
    45.         }
    46.     }
    47.  
    48.     // Update is called once per frame
    49.     void Update()
    50.     {
    51.         if (!pv.IsMine)
    52.             return;
    53.         Look();
    54.         Move();
    55.         Jump();
    56.  
    57.         for (int i = 0; i < items.Length; i++)
    58.         {
    59.             if(Input.GetKeyDown((i + 1).ToString()))
    60.             {
    61.                 EquipItem(i);
    62.                 break;
    63.             }
    64.         }
    65.  
    66.  
    67.         if (Input.GetAxisRaw("Mouse ScrollWheel") > 0f)
    68.         {
    69.             if(itemIndex >= items.Length - 1)
    70.             {
    71.                 EquipItem(0);
    72.             }
    73.             else
    74.             {
    75.                 EquipItem(itemIndex + 1);
    76.             }
    77.         }
    78.         else if(Input.GetAxisRaw("Mouse ScrollWheel") < 0f)
    79.         {
    80.             if(itemIndex <= 0)
    81.             {
    82.                 EquipItem(items.Length - 1);
    83.             }
    84.             else
    85.             {
    86.                 EquipItem(itemIndex - 1);
    87.             }
    88.         }
    89.     }
    90.  
    91.     void Look()
    92.     {
    93.         transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSens);
    94.  
    95.         verticalLookRot += Input.GetAxisRaw("Mouse Y") * mouseSens;
    96.         verticalLookRot = Mathf.Clamp(verticalLookRot, -90f, 90f);
    97.  
    98.         camHolder.transform.localEulerAngles = Vector3.left * verticalLookRot;
    99.     }
    100.  
    101.     void Move()
    102.     {
    103.         Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
    104.  
    105.         moveAmount = Vector3.SmoothDamp(moveAmount, moveDir * (Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : walkSpeed), ref smoothMoveVelocity, smoothTime);
    106.     }
    107.  
    108.     void Jump()
    109.     {
    110.  
    111.         if (Input.GetKeyDown(KeyCode.Space) && grounded)
    112.         {
    113.             rb.AddForce(transform.up * jumpForce);
    114.         }
    115.     }
    116.  
    117.     void EquipItem(int _index)
    118.     {
    119.         if (_index == previousItemIndex)
    120.             return;
    121.  
    122.         itemIndex = _index;
    123.  
    124.         items[itemIndex].itemGameObject.SetActive(true);
    125.  
    126.         if(previousItemIndex != -1)
    127.         {
    128.             items[previousItemIndex].itemGameObject.SetActive(false);
    129.         }
    130.  
    131.         previousItemIndex = itemIndex;
    132.  
    133.         if(pv.IsMine)
    134.         {
    135.             Hashtable hash = new Hashtable();
    136.             hash.Add("itemIndex", itemIndex);
    137.             PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
    138.         }
    139.     }
    140.  
    141.     public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
    142.     {
    143.         if(!pv.IsMine && targetPlayer == pv.Owner)
    144.         {
    145.             EquipItem((int)changedProps["itemIndex"]);
    146.         }
    147.     }
    148.  
    149.     public void SetGroundedState(bool _grounded)
    150.     {
    151.         grounded = _grounded;
    152.     }
    153.  
    154.     private void FixedUpdate()
    155.     {
    156.         if (!pv.IsMine)
    157.             return;
    158.         rb.MovePosition(rb.position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
    159.     }
    160. }
    161.  
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    I guess the script is not on an object with PhotonView and pv is null.
    If that's needed, make sure pv is available...

    MonoBehaviourPunCallbacks register for callbacks in OnEnable. Your code finds the photonView in Start, which is later. Property updates may happen in-between (and fail).
    The MonoBehaviourPunCallbacks have a photonView property, which provides the PhotonView anyways, so you actually don't need to find it (unless you look it up on another object).

    NullreferenceExceptions are usually something you need to debug yourself. They are very specific to your project (in most cases), so there is rarely a lot of help available...
     
  3. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i put the get component functions in awake and it works now!

    however, i get this error now

    Code (CSharp):
    1. Receive issue. State: Connected. Server: 'ns.exitgames.com' ErrorCode: 10054 SocketErrorCode: ConnectionReset Message: An existing connection was forcibly closed by the remote host.
    2.  
    3. UnityEngine.Debug:LogError (object)
    4. Photon.Realtime.LoadBalancingClient:DebugReturn (ExitGames.Client.Photon.DebugLevel,string) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2556)
    5. ExitGames.Client.Photon.PeerBase/<>c__DisplayClass108_0:<EnqueueDebugReturn>b__0 () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:1148)
    6. ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:438)
    7. ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1863)
    8. Photon.Pun.PhotonHandler:Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:221)
    9. Photon.Pun.PhotonHandler:FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:147)
    10.  
    this is one full error message btw
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    This is odd.
    Please try running one of our demos without modification. Does it happen, too?
    If so, read this and mail us (with the appid and info described in the page) to: developer@photonengine.com.