I've got a pretty simple setup since I've been stripping functions off of my initial build in trying to find the source of this issue.
Basically there are two classes- DesktopServer and RemoteClient (code below). The initial Scene has a TNManager object and a non-networked object that checks Application.Platform. If the runtime platform is the desktop (or editor) it instantiates a DesktopServer; if it's an iPhone it instantiates a RemoteClient. In either case, it calls
TNManager.StartUDP(Random.Range(10000, 50000));
before the instantiation occurs. The RemoteClient tries to connect to the first server it finds, which will always be the app running on the desktop.
85% of the time this works.
But 15% of the time, I get an EXC_BAD_ACCESS error and the iPhone app terminally crashes (attached) which from the Unity Answers forum I think is likely to be a null reference exception (or I would if there were any external references going on here).
Here are my classes:
using UnityEngine;
using System.Collections;
using TNet;
[RequireComponent
(typeof(TNObject
))] [RequireComponent
(typeof(TNUdpLobbyClient
))] public class DesktopServer : MonoBehaviour {
public int serverTcpPort = 5127;
// Use this for initialization
void Start ()
{
int udpPort = Random.Range(10000, 40000);
TNUdpLobbyClient lan = GetComponent<TNUdpLobbyClient>();
int lobbyPort = (lan != null) ? lan.remotePort : 0;
TNServerInstance.Start(serverTcpPort, udpPort, "server.dat", lobbyPort);
StartCoroutine(Connect());
}
IEnumerator Connect()
{
while(TNLobbyClient.knownServers.list.size == 0)
{
yield return 0;
}
TNet.ServerList.Entry ent = TNLobbyClient.knownServers.list[0];
TNManager.Connect(ent.externalAddress,ent.internalAddress);
while(!TNManager.isConnected)
{
yield return 0;
}
TNManager.JoinChannel(100,null);
}
void OnNetworkPlayerJoin (Player player)
{
Debug.Log("Player Joined");
}
}
and
using UnityEngine;
using System.Collections;
using TNet;
[RequireComponent
(typeof(TNObject
))] [RequireComponent
(typeof(TNUdpLobbyClient
))] public class RemoteClient : MonoBehaviour {
// Use this for initialization
void Start ()
{
TNManager.StartUDP(10000);
StartCoroutine(Connect());
}
IEnumerator Connect()
{
while(TNLobbyClient.knownServers.list.size == 0)
{
yield return 0;
}
TNet.ServerList.Entry ent = TNLobbyClient.knownServers.list[0];
//<<this is where the BAD_ACCESS occurs, based on Debug.Log calls>>
TNManager.Connect(ent.externalAddress,ent.internalAddress);
while(!TNManager.isConnected)
{
yield return 0;
}
TNManager.JoinChannel(100,null);
}
}
Am I missing something obvious? As far as I can tell I've mimicked all of the core sequencing from the demo's connecting structure, which itself appears to run flawlessly. I've tried deleting the TNet folder from the project and reinstalling it through the asset store just to make sure I'm running vanilla...