# Cannot get player to spawn

**URL:** <https://forum.heroiclabs.com/t/cannot-get-player-to-spawn/961>\
**Category:** Local Setup\
**Tags:** getting-started\
**Created:** [September 10, 2020, 6:21pm UTC](https://forum.heroiclabs.com/t/cannot-get-player-to-spawn/961 "2020-09-10T18:21:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![izhvnter](https://avatars.discourse-cdn.com/v4/letter/i/48db29/32.png) [@izhvnter](https://forum.heroiclabs.com/u/izhvnter)\
**Post date:** [September 10, 2020, 6:21pm UTC](https://forum.heroiclabs.com/t/cannot-get-player-to-spawn/961/1 "2020-09-10T18:21:40Z")

</div>

Hello,

I’ve been trying to get player to spawn upon joining a match by broadcasting a message to all presences then the client would call a method to spawn a character into the scene.

The problem is that I have separated methods (authentication, connecting to server, joining a match, etc…) and these methods return a Task (since I cannot “await void”) and because of that (as far as my understanding) these methods run in a background thread and therefore whenever I call a method that runs in the main thread i.g: “SpawnCharacter” Unity errors an exception “get\_transform can only be called from the main thread.”

here’s how a “SpawnCharacter” method look like:

```cs
public void SpawnCharacter(bool localPlayer)
{
    if (localPlayer)
    {
        Instantiate(playerPrefab, transform); // receives input locally
    }
    else
    {
        Instantiate(characterPrefab, transform); // receives input virtually
    }
}

```

and this is the method that I use to connect client’s to the server:

```cs
public async Task ConnectToServerAsync()
{
    socket = client.NewSocket();
    socket.Connected += () => Debug.Log("Connected to server.");
    socket.Closed += () => Debug.Log("Disconnected from server.");
    socket.ReceivedMatchState += newState =>
    {
        switch (newState.OpCode)
        {
            case (int) OP_CODES.SPAWN:
                gameController.SpawnCharacter( newState.UserPresence.UserId == session.UserId );
                break;
        }
    };
    await socket.ConnectAsync(session);
}

```

I’m not even sure what’s the actual issue here but if you know how to fix my problem then please do tell me.

---

<div class="post-metadata">

**Author:** ![zyro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/zyro/32/14_2.png) [@zyro](https://forum.heroiclabs.com/u/zyro)\
**Post date:** [September 11, 2020, 8:09am UTC](https://forum.heroiclabs.com/t/cannot-get-player-to-spawn/961/2 "2020-09-11T08:09:12Z")

</div>

@izhvnter Hi! This has been discussed on the forums a few times before. Unity restricts certain operations to being run on the main thread only.

The standard solution is to use a main thread dispatch pattern, like [this post describes](https://forum.heroiclabs.com/t/issue-of-unavailable-thread-context-for-main-thread-only-unity-functionalities-when-called-from-namama-socket-receiveloop-async-events/578).
