Marius Baud
Marius Baud

Reputation: 11

How to spawn gameObject on client using NetworkObject.spawn()?

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

code :

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

Upvotes: 1

Views: 228

Answers (0)

Related Questions