new scene trying to read custom properties but getting null reference

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.

new scene trying to read custom properties but getting null reference

siten0308
2021-10-22 18:29:19

Hello,

So i started a new scene, and when I start, I have the public void start() and trying to read from "CurrentRoom" property, but get the following error:

NullReferenceException: Object reference not set to an instance of an object

again, going from one scene to another, and the last scene works, and has all the properties filled out, or using the customproperties, for stats etc... but getting this, here is the code below:

public class GamePlay : MonoBehaviour

{

private int Choice2 = 1;

private Hashtable hash = new Hashtable();

public void Awake()

{

Choice2 = (int)PhotonNetwork.CurrentRoom.CustomProperties["Choice"];

Debug.Log(Choice2);

}

}

Comments

[Deleted User]
2021-10-22 19:41:13

Please make sure that CurrentRoom is not null. If it's null you can simply wait until it's filled in before getting the property.

siten0308
2021-10-22 19:49:41

gotya thanks MeepPun, how do I fill that object "CurrentRoom" with the room the last scene created?

[Deleted User]
2021-10-22 20:07:37

@siten0308 It should automatically fill whenever you join a Photon room. Make sure that you're not disconnecting from Photon whenever you change scenes. If you are disconnecting then make sure you rejoin to the room. Also CurrentRoom does not fill immediately, you need a response from the server, so there can be a few hundred milliseconds-ish of latency between it.

siten0308
2021-10-22 20:57:13

gotya weird... its showing null even waiting 10 seconds:

this is the current code full:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using Photon.Pun;

public class GamePlay : MonoBehaviour

{

public GameObject defaultBackground;

public float timeRemaining = 10.0f;

public GameObject message_txt;

// Start is called before the first frame update

void Start()

{

message_txt.SetActive(true);

defaultBackground.SetActive(true);

}

public void Update()

{

if(timeRemaining > 0)

{

timeRemaining -= Time.deltaTime;

}

else

{

message_txt.SetActive(false);

defaultBackground.SetActive(false);

timeRemaining = 10.0f;

Debug.Log(PhotonNetwork.CurrentRoom.CustomProperties["Choice"]);

}

}

}

after the timer, the Else does work, but again, sadly the PhotonNetwork.CurrentRoom shows null :(

also, i dont think I am disconnecting when going from scene to scene, here is the start game button method:

public void OnClick_StartGame()

{

roomOptions.CustomRoomProperties = new Hashtable() { { "House", housechoice }, { "Difficulty", difficultychoice }, { "Noise", 0 } };

PhotonNetwork.LoadLevel("House");

}

siten0308
2021-10-22 21:37:10

OK finally found the issue, sorta of...

So first thing, I need to make a hashtable:

public void OnClick_StartGame()

{

Hashtable additionPro = new Hashtable() { { "Choice", choice }};

PhotonNetwork.CurrentRoom.SetCustomProperties(additionPro, null, null);

PhotonNetwork.LoadLevel("Level1");

}

so now I see it in the NEW scene... HOWEVER, if I put:

void Start()

{

message_txt.SetActive(true);

defaultBackground.SetActive(true);

Debug.Log(PhotonNetwork.CurrentRoom.CustomProperties);

}

it comes out like:

(System.String)curScn=(System.String)Main

which doesn't show the custom properties set from the last scene... HOWEVER, if i do:

public float timeRemaining = 2.0f;

public void Update()

{

if (timeRemaining > 0)

{

timeRemaining -= Time.deltaTime;

}

else

{

message_txt.SetActive(false);

defaultBackground.SetActive(false);

timeRemaining = 2.0f;

Debug.Log(PhotonNetwork.CurrentRoom.CustomProperties);

}

}

This i see the custom properties, as you mentioned... however is the best work around is to have a.... "Loading screen" sign for about 2-5 seconds in the update method like I sorta have it?

Back to top