NetworkObject.spawn() does not work !

NetworkObject.spawn() does not work ! I’m using unity netcode for gameObjects and I try to spawn a gameObjects by instantiating it with the host and then use getComponent().spawn() to instantiate it on the client but that does not work ! The gameObject does not spawn on the client but when I Do “print(NetworkObject.isSpawned)” (on the host) it is returning true. the prefab I instantiate on the host has a networkObject component and is in the network prefab list of the networkManager and the attached script is a networkBeahviour.

I try to find the answer to my question on forum but all of them say that getComponent().spawn() is the way to do. I want to do things like that because i want to sync the transform of the object on client using NetworkTransform component. I’m also using unity relay for transport and Unity lobby ( maybe that could be the problem I don’t know ) Thanks for the help

GameObject newRock = Instantiate(RockPrefab, transform);
if (GameManager.instance.IsPlayingMultiplayer)
{
   newRock.GetComponent<NetworkObject>().Spawn(true);
}
1 Like

That condition is likely to be incorrect. It needs to check if the local instance is either host or server, not whether multiplayer is currently commencing. I’m guessing this causes Spawn to be run for both host and client but for the client this will log as an exception because only the server/host can spawn network objects.

Try to confirm that by having the Console open for both host and client.

In case you weren’t aware, you can use ParrelSync to avoid having to make builds all the time and be able to monitor both host and client in the Unity editor.

1 Like

Thanks for your answer but this code only run on the host

if(IsHost)
{
      InstantiateRock();
}

void InstantiateRock()
{
        GameObject newRock = Instantiate(RockPrefab, transform);
        if (GameManager.instance.IsPlayingMultiplayer)
        {           newRock.GetComponent<NetworkObject().Spawn(true);
        }

I already using ParrelSync to test :slight_smile:
when I start the game and spawn the prefab on the host side, nothing happen on the client side and nothing appears in the console

Does this code need to be call on the object owned by the player ?

Did you check that Spawn gets called? IsPlayingMultiplayer may be false.

1 Like

Yes IsPlayingMultiplayer is true and when I call print(NetworkObject.isSpawned) that print true.
When I began coding the multiplayer for my game, I used this method and that was working, then I stop using it and I added unity relay and Unity lobby for the multiplayer, do you think this could be causing this problem?

If you can quickly test it locally without using the relay/lobby services I would give that a shot.
If the spawn happens immediately after launching the game respectively connecting, this could also be a timing issue. Try hooking up the spawn to a keyboard action to see if it’ll work at any later time.

1 Like

I tested without using relay/lobby and there is the issues ( the obj don’t spawn on the client side and on the host side that print true for print(NetworkObject.isSpawned) ).
I also tried to hooking up the spawn to a keyboard action but that doesn’t resolve the issue.

Hmm maybe try making a simple barebones example for spawning.
Could be something that’s not easy to spot, perhaps the prefab isn’t actually in the list because the RockPrefab assignment references a different prefab or something like that.

1 Like

I solved the problem !!! I just needed to set the “Spawn with observer” of the networkObject to true, this is a very silly error I’ve been on for over a day :(, thanks for taking your time to help me out :).

2 Likes