0

I am trying to create a script which changes materials of players which join over network. When I run the script, I get a null reference exception error on the photon rpc function call.

This is my script:

using UnityEngine;
using Photon.Realtime;

public class ChangeStarterMaterial : Photon.MonoBehaviour
{
    
    //~Arrays:
    [SerializeField]
    private GameObject[] playersInGame;
    [SerializeField]
    private Material[] materials;

    //~PhotonView:
    private PhotonView view;

    private void Start()
    {

        view = GetComponent<PhotonView>();//Get view

        playersInGame = GameObject.FindGameObjectsWithTag("Player");

    }

    private void Update()
    {

        view.RPC("AssignColors" , PhotonTargets.AllBuffered);

    }

    [PunRPC]
    public void AssignColors()
    {

        if(PhotonNetwork.isMasterClient)
        {
            for(int i = 0; i < playersInGame.Length ; i++)
            {

                //Get MainPlayerBody Mat:
                playersInGame[i].transform.Find("MainPlayer").GetComponent<SkinnedMeshRenderer>().material = materials[i];
                //Get Visor Mat:
                playersInGame[i].transform.Find("MainPlayer").transform.Find("Back Pack").GetComponent<SkinnedMeshRenderer>().material= materials[i];

            }
        }

    }

}

I am new to photon engine so please help.

Thanks, Raghav Gohil

  • thanks for responding but I am referring to photon engine rpc functions.. – Raghav Gohil Nov 22 '20 at 08:57
  • _"thanks for responding but I am referring to photon engine rpc functions"_ - so _"null reference exception[s]"_ are not the sole domain of _Photon_ but rather .NET in general. Looking at your code did you check to see if `view` was `null`? Unity's [`GetComponent`](https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html) can return `null`. That's what the link above is for –  Nov 22 '20 at 09:01
  • Thanks mate!!! So the solution is that the gameobject didn't have a photon view component. – Raghav Gohil Nov 22 '20 at 09:13
  • 1
    Not a problem good sir –  Nov 22 '20 at 09:17

0 Answers0