RPC Photon Pun 2022 not working?

Hello everyone! :slight_smile: I am working on a multiplayer Photon game and I ran into a little bug while testing, my change color script is not working! I get the error “Object reference not set to an instance of an object” on “if(photonView.IsMine)”, why?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class ChangeColor2 : MonoBehaviour
{
   
    public Material playerCharacterMat;
   
    public Material playerMat;

    private PhotonView photonView;

    public GameObject networkPlayer;


    void Start()
    {
    playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
    photonView = networkPlayer.GetComponent<PhotonView>();
    }

    public void RedColorChange()
    {

          playerMat.color = new Color32(255,14, 0, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 1);
    }
    }

    public void GreenColorChange()
    {

    playerMat.color = new Color32(36,255, 0, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 2);
    }
    }

    public void BlueColorChange()
    {

    playerMat.color = new Color32(23,0, 255, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 3);
    }
    }

    public void YellowColorChange()
    {

    playerMat.color = new Color32(255,246, 0, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 4);
    }
    }

    public void OrangeColorChange()
    {

    playerMat.color = new Color32(255,104, 0, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 5);
    }
    }

    public void TealColorChange()
    {

    playerMat.color = new Color32(0,255, 221, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 6);
    }
    }

    public void PurpleColorChange()
    {

    playerMat.color = new Color32(164,0, 255, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 7);
    }
    }

    public void PinkColorChange()
    {

    playerMat.color = new Color32(255,0, 176, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 8);
    }
    }

    public void WhiteColorChange()
    {

    playerMat.color = new Color32(255,255, 255, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 9);
    }
    }

    public void BlackColorChange()
    {

    playerMat.color = new Color32(46,46, 46, 255);

    if(photonView.IsMine)
    {
        photonView.RPC("ChangeTexture", RpcTarget.All, 10);
    }
    }

    [PunRPC]
    void ChangeTexture (int choice){

    if(choice == 1)
    {
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(255,14, 0, 255);
    }else if(choice == 2){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(36,255, 0, 255);
    }else if(choice == 3){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(23,0, 255, 255);
    }else if(choice == 4){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(255,246, 0, 255);
    }else if(choice == 5){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(255,104, 0, 255);
    }else if(choice == 6){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(0,255, 221, 255);
    }else if(choice == 7){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(164,0, 255, 255);
    }else if(choice == 8){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(255,0, 176, 255);
    }else if(choice == 9){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(255,255, 255, 255);
    }else if(choice == 10){
        playerCharacterMat = this.gameObject.GetComponent<SkinnedMeshRenderer>().materials[1];
        playerCharacterMat.color = new Color32(46,46, 46, 255);
    }


    }
}

I have the same issue, it was working fine up to a certain point

Make your own thread, it either because

  1. You haven’t assigned the photonView component to the script
  2. The GO your using the RPC does not have a photonView component

Actually you NEVER need to post for NullReferenceError. Why?

Because the answer is ALWAYS the same three steps.

None of those steps involve posting anywhere.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that