Hello everyone! 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);
}
}
}