photonView is null?
The whole answer can be found below.
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).
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.
Thanks for updating this and providing an answer!
Back to top