0

So i'm trying to make a vr multiplayer game and i get 2 errors when i start the game. 1.:NullReferenceException: Object reference not set to an instance of an object NetworkPlayer.Start () (at Assets/NetworkPlayer.cs:28) I only get it once. 2.:NullReferenceException: Object reference not set to an instance of an object NetworkPlayer.MapPosition (UnityEngine.Transform target, UnityEngine.Transform rigTransform) (at Assets/NetworkPlayer.cs:50) NetworkPlayer.Update () (at Assets/NetworkPlayer.cs:42) I get them every frame. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using Photon.Pun;
using UnityEngine.XR.Interaction.Toolkit;

public class NetworkPlayer : MonoBehaviour
{

    public Transform head;
    public Transform leftHand;
    public Transform rightHand;
    private PhotonView photonView;
    private Transform headRig;
    private Transform leftHandRig;
    private Transform rightHandRig; 


    // Start is called before the first frame update
    void Start()
    {
        photonView = GetComponent<PhotonView>();

        //XRRig rig = FindObjectOfType<XRRig>();
        //GetComponent<XROrigin>();
        XRRig rig = GetComponent<XRRig>();
        headRig = rig.transform.Find("Camera Offset/Main Camera");
        leftHandRig = rig.transform.Find("Camera Offset/LeftHand Controller");
        rightHandRig = rig.transform.Find("Camera Offset/RightHand Controller");
    }

    // Update is called once per frame
    void Update()
    {
        if (photonView.IsMine)
        {
            rightHand.gameObject.SetActive(false);
            leftHand.gameObject.SetActive(false);
            head.gameObject.SetActive(false);

            MapPosition(head, headRig);
            MapPosition(leftHand, leftHandRig);
            MapPosition(rightHand, rightHandRig);
        }   
    }

    void MapPosition(Transform target,Transform rigTransform)
    {
        target.position = rigTransform.position;
        target.rotation = rigTransform.rotation;
    }


    
}

I think it might be a problem with using and XRRig.

Please help, Happy Hollidays.

  • Sounds like `GetComponent()` is already returning `null` -> there is no such component on the same object as your `NetworkPlayer` .. the rest are follow up errors since none of your other fields will be assigned. Btw going through `rig.transform` is quite redundant anyway ... **If** the component is on the same object anyway then `transform` and `rig.transform` point to the same reference ... If it is somewhere nested below you might be luckier using `GetComponentInChildren(true)` – derHugo Dec 22 '21 at 20:23

0 Answers0