Observed type is not serializable: UnityEngine.BoxCollider2D

Options
Hi there Im having a problem when I have 2 players connected to my game.

// This error is also causing my players health which is dependent on the OnTriggerEnter function of my gameObject not to work.

When spawning my 2nd player my console gets spammed with this error

Observed type is not serializable: UnityEngine.BoxCollider2D
UnityEngine.Debug:LogError(Object)
PhotonView:SerializeComponent(Component, PhotonStream, PhotonMessageInfo) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:523)
PhotonView:SerializeView(PhotonStream, PhotonMessageInfo) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:352)
NetworkingPeer:OnSerializeWrite(PhotonView) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:4243)
NetworkingPeer:RunViewUpdate() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:4105)
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:165)


My NetController Script handling the OnPhotonSerializeView:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetController : Photon.MonoBehaviour {

    Vector3 realPos;
    Quaternion realRot;
    float lastUpdateTime = 0.9f;
    //BoxCollider2D boxCollider;



   [Header("Animator")]
    Animator anim;

    private void Start()
    {
        anim = GetComponent<Animator>();
       // boxCollider = GetComponent<BoxCollider2D>();
        if (anim == null)
        {
            Debug.LogWarning("No animator component on this gameobject.");
        }
    }
    void Update () {

        if (photonView.isMine)
        {
            //Do nothing as the controller script is moving our player.
          
        }

        else
        {
           
            transform.position = Vector3.Lerp(transform.position, realPos, lastUpdateTime * Time.deltaTime);
            
            transform.rotation = Quaternion.Lerp(transform.rotation, realRot, lastUpdateTime * Time.deltaTime);
        }
		
	}

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        
        if (stream.isWriting)
        {
            //This is our player
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(anim.GetFloat("Speed"));
          //  stream.SendNext(boxCollider);


        }

        else
        {
            // This is someone else's player. We need to recieve  their position and update our version of this player.
            realPos = (Vector3) stream.ReceiveNext();
            realRot = (Quaternion)stream.ReceiveNext();
            

            //Animations
            anim.SetFloat("Speed", (float)stream.ReceiveNext());
        }
    }
}

Ive made a copy of the project before this error showed up.It seems to be working all fine there (the duplicate of my project). On inspection of the copy of the project looking at the components and the order in the Inspector along with the NetController.cs in that project there is no change(or at least not visible) . Yet im encountering no issue as mentioned above in that copy. Please any help will be great. Been struggling with this for a while now.
Please let me know if there is anything else I could explain.


Comments

  • [Deleted User]
    Options
    Hi @Ruben,

    BoxCollider2D is neither one of the default types nor one of the already registered Custom Types. To synchronize them, you have to register it as a Custom Types yourself. You can find an example how Unity's Vector2 is serialized in PUN at the bottom of this page.

    The reason why the error messages occur after the second client has joined the room is because OnPhotonSerializeView doesn't get called when there is just one client in the room.