How do I properly deal with Internal_CloneSingle can only be called from the main thread
So I've developed my own network protocol built upon the already existing protocol, TCP.
I'm now finally implementing it to my game which is not a complex game at all. All it's going to do, is spawn in the player when the client connects to the server.
The issue I'm facing now is when I try to call Instantiate();
it throws the exception:
UnityEngine.UnityException: Internal_CloneSingle can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
at (wrapper managed-to-native) UnityEngine.Object.Internal_CloneSingle(UnityEngine.Object)
at UnityEngine.Object.Instantiate[T] (T original) [0x00017] in C:\buildslave\unity\build\Runtime\Export\UnityEngineObject.bindings.cs:275
at GameManager.SpawnPlayer (System.Int32 id, System.String username) [0x00023] in C:\Users\user\Documents\Astaria\Assets\Scripts\GameManager.cs:37
at ClientHandleData.SpawnPlayer (System.Byte[] data) [0x000a9] in C:\Users\user\Documents\Astaria\Assets\Scripts\ClientHandleData.cs:166
at ClientHandleData.HandlePacket (System.Byte[] data) [0x00031] in C:\Users\user\Documents\Astaria\Assets\Scripts\ClientHandleData.cs:95
at ClientHandleData.HandleData (System.Byte[] data) [0x000ef] in C:\Users\user\Documents\Astaria\Assets\Scripts\ClientHandleData.cs:66
at GameNet.OnReceive (System.IAsyncResult ar) [0x0003c] in C:\Users\user\Documents\Astaria\Assets\Scripts\GameNet.cs:62
UnityEngine.Debug:LogError(Object)
GameNet:OnReceive(IAsyncResult) (at Assets/Scripts/GameNet.cs:70)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()
I have this empty object in my scene called GameManager
in which I've attached the GameManager
script to and it looks like this
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public static Dictionary<int, Client> players = new Dictionary<int, Client>();
public GameObject localPlayerPrefab;
public GameObject playerPrefab;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(this);
}
}
public void SpawnPlayer(int id, string username)
{
//Instantiate can only be called from the main thread..
GameObject _player;
if (id == Client.instance.clientId)
_player = Instantiate(localPlayerPrefab);
else
_player = Instantiate(playerPrefab);
players.Add(id, _player.GetComponent<Client>());
Debug.Log($"PLAYER WITH THE ID: {id} HAS CONNECTED!");
}
}
And that function gets called from my ClientHandleData.cs
private void SpawnPlayer(byte[] data)
{
Packet packet = new Packet();
packet.WriteBytes(data);
var clientId = packet.ReadInt();
var username = packet.ReadString();
//Spawn the connected player on my screen
GameManager.instance.SpawnPlayer(clientId, username);
}
Which is defined in a dictionary at the top of the class
public delegate void Packet_(byte[] data);
private Dictionary<byte, Packet_> packets;
public void InitializePackets()
{
Debug.Log("Initializing Network Packets..");
packets = new Dictionary<byte, Packet_>();
packets.Add((byte)GameOpCodes.PlayerData, SpawnPlayer);
}
The function InitializePackets
gets called from Client.cs
which looks like this
public class Client : MonoBehaviour
{
public static ClientHandleData HandleData = new ClientHandleData();
public static GameNet client;
public string username;
public static Client instance;
private void Awake()
{
instance = this;
}
private void Start()
{
HandleData.InitializePackets();
client = new GameNet();
client.ConnectToServer();
}
}
And the way the Client receives data is from GameNet.cs
which has a socket callback that says OnReceive
Which then runs this
Client.HandleData.HandleData(myBytes);
And that's how it's all tied together.
Why is it throwing the exception and how do I properly deal with it?
Your answer
Follow this Question
Related Questions
Spawn Objects in a Server UNet 0 Answers
Network Instantiation works on one command and does not work on another 0 Answers
i can't see other player with photon cloud server,I Can't see each other player in photon cloud 2 Answers
Client's object spawn position and rotation network bug? 0 Answers
Bullets not spawning in other clients but its spawning on the client that shot it. 1 Answer