• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by vargadevelopment · Aug 29, 2020 at 12:43 PM · networkinginstantiatenetworkinstantiate prefabthreads

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

306 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Spawn Objects in a Server UNet 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges