Why am I getting a null reference exception error?
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).
Why am I getting a null reference exception error?
Taran
2020-04-15 15:23:02
When I play my scene I receive this error (at this point I haven't connected/attempted a connection to Photon) -
NullReferenceException : Object not set to an instance of an object. Photon.Pun.PhotonNetwork.AddCallbackTarget (System.Object target) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:85)
This error is stopping me from connecting to photon. Anyone know how I can fix this?
Comments
Hi @Taran,
Thank you for choosing Photon!
What PUN version do you use?
What is the full stack trace of the error?
What are the minimal repro steps? could you post a short snippet of where or how you use AddCallbackTarget?
Hi @JohnTube,
I am using PUN2. Regarding the error, I have noticed that it occurs every other time I play the scene. So I can play the scene and the error doesn't appear and I can connect to photon and everything seem to be working fine. Then the next time I play the scene the error appears and I cannot connect. This process seem to repeat itself infinitely. The code/error for the AddCallbackTarget isn't in any of my created scripts but is coming from PhotonNetworkPart (script which has come with Photon). Here is the code snippet from that script:
public static void AddCallbackTarget(object target)
{
if (target is PhotonView)
{
return;
}
IPunOwnershipCallbacks punOwnershipCallback = target as IPunOwnershipCallbacks;
if (punOwnershipCallback != null)
{
OnOwnershipRequestEv += punOwnershipCallback.OnOwnershipRequest;
OnOwnershipTransferedEv += punOwnershipCallback.OnOwnershipTransfered;
}
NetworkingClient.AddCallbackTarget(target);
}
Believe this is the full stack trace of the error:
NullReferenceException: Object reference not set to an instance of an object
Photon.Pun.PhotonNetwork.AddCallbackTarget (System.Object target) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:85)
Photon.Pun.MonoBehaviourPunCallbacks.OnEnable () (at Assets/Photon/PhotonUnityNetworking/Code/PunClasses.cs:225)
What do you mean by the minimal repro steps?
hi @Taran,
You have a class that is extending MonoBehaviourPunCallbacks.
Somehow that component is being destroyed in Awake before OnEnable?
Okay. The class I have which extends MonoBehaviorPunCallbacks doesn't call the awake method (and neither does any other class within my scene) so that's a bit strange. I am going to move my script into a new clean project will only PUN2 downloaded and see whether or not the issue still persists.
I see the issue now.
First time we get such report.
PhotonNework.NetworkingClient is still null when PhotonNetwork.AddCallbackTarget is called which is not expected as the NetworkingClient is set in the constructor when the singleton instance is first created.
We will investigate.
Thanks for your report and patience!
Hi! I've started to have same issue after I've switched to Compute Engine VM (not sure is that related)
NullReferenceException: Object reference not set to an instance of an object
Photon.Pun.PhotonNetwork.AddCallbackTarget (System.Object target) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:85)
Photon.Pun.MonoBehaviourPunCallbacks.OnEnable () (at Assets/Photon/PhotonUnityNetworking/Code/PunClasses.cs:225)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
I've also updated Pun2 asset to the latest version. My class extending MonoBehaviourPunCallbacks and here the Start method
void Start()
{
if (!PhotonNetwork.IsConnected)
{
PhotonNetwork.AutomaticallySyncScene = true;
PhotonNetwork.GameVersion = gameVersion;
PhotonNetwork.ConnectUsingSettings();
}
Debug.Log(PhotonNetwork.LocalPlayer.UserId + " pid in start");
progressLabel.SetActive(false);
//controlPanel.SetActive(true);
}
In additional to that I have this error as well
NullReferenceException: Object reference not set to an instance of an object
Photon.Pun.PhotonNetwork.set_GameVersion (System.String value) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetwork.cs:87)
Launcher.Start () (at Assets/Scripts/Launcher.cs:98)
where Launcher.cs:98 is PhotonNetwork.GameVersion = gameVersion; from start method
@albo , @Taran , which version of Unity is that?
Any other exceptions in the logs? Please send a full log and the full class, which causes the error. So we can maybe repro.
You can mail us: [email protected]
@Taran wrote: »
Hi @JohnTube,
I am using PUN2. Regarding the error, I have noticed that it occurs every other time I play the scene. So I can play the scene and the error doesn't appear and I can connect to photon and everything seem to be working fine. Then the next time I play the scene the error appears and I cannot connect. This process seem to repeat itself infinitely. The code/error for the AddCallbackTarget isn't in any of my created scripts but is coming from PhotonNetworkPart (script which has come with Photon). Here is the code snippet from that script:
public static void AddCallbackTarget(object target)
{
if (target is PhotonView)
{
return;
}IPunOwnershipCallbacks punOwnershipCallback = target as IPunOwnershipCallbacks; if (punOwnershipCallback != null) { OnOwnershipRequestEv += punOwnershipCallback.OnOwnershipRequest; OnOwnershipTransferedEv += punOwnershipCallback.OnOwnershipTransfered; } NetworkingClient.AddCallbackTarget(target); }
Believe this is the full stack trace of the error:
NullReferenceException: Object reference not set to an instance of an object
Photon.Pun.PhotonNetwork.AddCallbackTarget (System.Object target) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:85)
Photon.Pun.MonoBehaviourPunCallbacks.OnEnable () (at Assets/Photon/PhotonUnityNetworking/Code/PunClasses.cs:225)What do you mean by the minimal repro steps?
This has just happened to me as well - updated to the latest version of PUN2 and every other time I hit play I get the same null reference exception. Occurred on both 2019.3.3f1 and 2019.3.12.f1.
I rolled back to the old package and the issue immediately went away. I have a very basic setup integrating PlayFab authentication (pretty much just followed the tutorials).
I will stay on the old version for now but thought you may be interested to know it is still occurring.
Cheers.
@doug_h, I thought a colleague would reply. Yes, we are interested in this.
We could use a repro (via download link to [email protected]), so if you could share a project or such, we'd have a look. So far, we could not narrow it down.
Did you disable "Domain Reloading" maybe?
https://docs.unity3d.com/Manual/DomainReloading.html
@doug_h , @albo, I did not find a way to repro this yet, so we release PUN 2.19 without any fixes for this.
If you run into this again, it would help a lot if you could get us the full error logs and a repro case. Mail to: [email protected] to send a link to a project that reproduces it.
inventonater
2020-07-06 15:54:28
@Tobias wrote: »
Did you disable "Domain Reloading" maybe?
https://docs.unity3d.com/Manual/DomainReloading.html
Thank god for this post - Domain Reloading was exactly the problem for me. Was so confused why everytime I created a new project the issue wouldn't exist.. but in my real project it would break.. it was because I disabled Domain Reloading!
Back to top