photonView is null?

I'm trying to call RPC from the script, but it throws a null reference exception.
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;

public class HitVFX : MonoBehaviourPun {
    [SerializeField] private Slider m_HPSlider = null;
    private Health m_Health = null;

    public Health Health {
        get {
            if (m_Health == null) {
                m_Health = GetComponent<Health>();
            }

            return m_Health;
        }
    }

    void Start() {
        m_Health = GetComponent<Health>();
        Initialize();
    }

    public void Initialize() {
        if (m_HPSlider) {
            SetHPSliderMinMaxValues();
            UpdateHPSlider(Health.Value);
        }
    }

    public void SetHPSliderMinMaxValues() {
        if (m_HPSlider == null) {
            return;
        }
        
        float maxHealth = Health.MaxValue;
        SetHPSliderMinMaxValuesInternal(maxHealth);

        print(photonView); // NULL HERE
        photonView.RPC("SetHPSliderMinMaxValuesInternal", RpcTarget.Others, maxHealth); // THROWING NULL REFERENCE EXCEPTION
    }

    [PunRPC]
    void SetHPSliderMinMaxValuesInternal(float maxValue) {
        m_HPSlider.minValue = 0;
        m_HPSlider.maxValue = maxValue;
    }
}

Tried refreshing the RPC list in resources doesn't change anything. Using Unity 2019.1.0f2 and PUN 2.33 Lib 4.1.6.3.

Best Answer

  • modernator
    Answer ✓
    Nevermind, caused by different gameobject using the same component.

Answers

  • Note that the object that has this component is instantiated by PhotonNetwork.Instantiate.
  • modernator
    Answer ✓
    Nevermind, caused by different gameobject using the same component.
  • Thanks for updating this and providing an answer!