In one cs file I defined a class named “ConnectServer” which connected to the server:
public event EventHandler Changed;
BeginRecieve(){
...
AsyncCallback callback = new AsyncCallback(OnData);
connection = socket.BeginReceive(coords, 0, coords.Length, SocketFlags.None, callback, null);
}
private void OnData() {
GPSDATA gps = (GPSDATA)BytesToStuct(coords, typeof(GPSDATA));
gpslist.Add(gps);
if (Changed != null)
Changed(this, EventArgs.Empty);
}
In the other Script:
public Transform trans;
void Start(){
cs = new ConnectServer();
cs.BeginRecieve();
cs.Changed += OnAddData;
}
OnAddData(){
Instantiate(trans);
}
but It can not work and occured an error:
UnityEngine.Object:Internal_CloneSingle(Object)
UnityEngine.Object:Instantiate(Object) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Runtime\ExportGenerated\Editor\BaseClass.cs:65)
ConnectServerTest:OnAddData(Object, EventArgs) (at Assets\ConnectServerTest.cs:30)
ConnectServer:OnData(IAsyncResult) (at Assets\ConnectServer.cs:183)
System.Net.Sockets.SocketAsyncResult:Complete()
System.Net.Sockets.Worker:Receive()
I don’t know why this happen while It can be Instantiate successfully when I don’t use the network.