Most confusing errors ever

I'm not sure if it's related to Photon or not, but I'm having one hell of a time with a couple random errors. Everything I was testing was "working", with no errors thus far. (Working is in quotes only because obviously, something is wrong).

The debugger doesn't catch this until I actually run the game, and even then it only stops me from connecting to a random room about half the time. If I do connect, at least one script wont work because of a null reference. However, this is the code that is having a null reference (Keep in mind this was working about an hour ago!):

[code2=csharp]void OnJoinedRoom()
{
StartCoroutine("RepeatArrayGrab");

}

IEnumerator RepeatArrayGrab()
{


print("First try");
weaponSys = PlayersArray.playerList[PhotonNetwork.player.ID].GetComponent<WeaponSystem>();
yield return new WaitForSeconds (.2f);
if(weaponSys == null){
print ("retry");
StartCoroutine("RepeatArrayGrab");}
else
print ("Accessed "+PhotonNetwork.player.ID+" as our position.");
print (PlayersArray.playerList);
}[/code2]

If the game doesn't freeze, I always get a "First try" read out. Never get a "Retry" response (even though this is always null).

I get this error from a photon script:

NullReferenceException: Object reference not set to an instance of an object
PhotonNetwork.Connect (System.String serverAddress, Int32 port, System.String appID, System.String gameVersion) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:859)
PhotonNetwork.ConnectUsingSettings (System.String gameVersion) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:809)
NetworkManager.Start () (at Assets/Scripts/Manager Scripts/NetworkManager.cs:12)


which points to:
[code2=csharp]public static void Connect(string serverAddress, int port, string appID, string gameVersion)
{
if (port <= 0)
{
Debug.LogError("Aborted Connect: invalid port: " + port);
return;
}

if (serverAddress.Length <= 2)
{
Debug.LogError("Aborted Connect: invalid serverAddress: " + serverAddress);
return;
}

if (networkingPeer.PeerState != PeerStateValue.Disconnected) //POINTS TO THIS LINE
{
Debug.LogWarning("Connect() only works when disconnected. Current state: " + networkingPeer.PeerState);
return;
}

if (offlineMode)
{
offlineMode = false; // Cleanup offline mode
Debug.LogWarning("Shut down offline mode due to a connect attempt");
}

if (!isMessageQueueRunning)
{
isMessageQueueRunning = true;
Debug.LogWarning("Forced enabling of isMessageQueueRunning because of a Connect()");
}

serverAddress = serverAddress + ":" + port;

//Debug.Log("Connecting to: " + serverAddress + " app: " + uniqueGameID);
networkingPeer.mAppVersion = gameVersion + versionPUN;
networkingPeer.Connect(serverAddress, appID);
}[/code2]

I have a NetworkManager script that basically gives the player their character and assigns their character to a static array. The array is used later for the GUI, which determines which player to give upgrades to. The NetworkManager script has a debug line that spits out what their player ID is, and this comes moments after the "First try" read out. I've tried changing the order of the scripts in the script execution thing but that hasn't changed anything. I'm confident that the code is correct as I don't remember changing anything, but clearly I accidentally did something.

Edit: I don't yet have any logic for when a player disconnects. Could this error be caused by frequent start/stops of the game (hence the every other try problem)?

Edit2: this doesn't appear to be the problem as the compiler catches a null reference when the game is being built.

NullReferenceException: Object reference not set to an instance of an object
WeaponSystem..ctor ()
UnityEditorInternal.InternalEditorUtility:GetGameObjectInstanceIDFromComponent(Int32)
UnityEditor.DockArea:OnGUI()

Double clicking the error takes me to my player prefab (which holds the WeaponSystem script).

Comments

  • The exception code you pasted points to line 859. In the copied code, I can't see which line that is.
    Check which variables are used in that line and make sure you provide them as non-null when calling ConnectUsingSettings(). Also your ServerSettings.asset must be somewhere in the project in a Resources folder.
  • Okay, I'm trying very hard to debug what went wrong.

    NullReferenceException: Object reference not set to an instance of an object
    PhotonNetwork.Connect (System.String serverAddress, Int32 port, System.String appID, System.String gameVersion) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:859)
    PhotonNetwork.ConnectUsingSettings (System.String gameVersion) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:809)
    NetworkManager.Start () (at Assets/Scripts/Manager Scripts/NetworkManager.cs:13)

    translates to:

    [code2=csharp]if (networkingPeer.PeerState != PeerStateValue.Disconnected)
    {
    Debug.LogWarning("Connect() only works when disconnected. Current state: " + networkingPeer.PeerState);
    return;
    }[/code2]

    And knowing that (literally) every other try fails I'm lead to ask the question: why am I not disconnected until I reconnect to the cloud?

    So, I thought maybe Photon was holding some old information longer than what I'd expect. So I waited. That didn't do anything.

    So, I ran my game from within unity and when it ran successfully I stopped the game and closed Unity. After opening it back up and repeating the process it ran fine. Aha, I said! But, it is a nullreference in

    [code2=csharp]public static void Connect(string serverAddress, int port, string appID, string gameVersion)[/code2]

    But, I'm in the cloud. So, the serverAddress and port are mostly taken care of without me after the initial setup, right? the address, port and appID are all set in the PhotonServerSettings asset which is in a resources folder. and my gameversion is still being used when connecting.
  • Line 859 is:
    [code2=csharp]if (networkingPeer.PeerState != PeerStateValue.Disconnected)[/code2]

    The error is due to a NullReference and the only variable in that line that could is nullable is networkingPeer.
    So the actual question is not why you're not disconnected but why the networkingPeer variable is null.
    It shouldn't, by all means, as all of Photon Unity Networking relies on this not being null. The PUN code never sets this to null but line 742 sets it to a new NetworkingPeer. So it's unclear why this is never set in your case.

    I'd say: Re-import PUN from the Asset Store. In some odd cases, this helped.
    Otherwise look up where you use networkingPeer and if your code sets it to null. You can also add a debug line before and after the networkingPeer is set to check if both are called and if there is a related issue.