photonView is null?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

photonView is null?

modernator
2021-07-28 08:37:57

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.

Comments

modernator
2021-07-28 08:40:55

Note that the object that has this component is instantiated by PhotonNetwork.Instantiate.

modernator
2021-07-28 11:08:31

Nevermind, caused by different gameobject using the same component.

Tobias
2021-07-28 11:33:55

Thanks for updating this and providing an answer!

Back to top