1
\$\begingroup\$

I'm making a game with UNet where the server must create some objects when he connects to the game, and all clients play with these objects that the server has created.

  1. A server connects to the game
  2. In his start method he calls a function CmdbuildEverything to spawn some objects to the game.
  3. A local player connects to the game. The local player should be able to see the objects that the server player has created.

The code:

public class GameScript : NetworkBehaviour {
    void Start() {
        if(base.isServer)
            CmdbuildEverything();
    }
    [Command]
    void CmdbuildEverything() {
        GameObject ob1 = (GameObject)Instantiate(Resources.Load("Temple"), new Vector3(0, 0, 1), Quaternion.identity);
        GameObject ob2 = (GameObject)Instantiate(Resources.Load("Temple"), new Vector3(0, 1, 0), Quaternion.identity);
        GameObject ob3 = (GameObject)Instantiate(Resources.Load("Temple"), new Vector3(1, 0, 0), Quaternion.identity);
        NetworkServer.Spawn(ob1);
        NetworkServer.Spawn(ob2);
        NetworkServer.Spawn(ob3);
    }
}

The problem is that the objects are created only on the server player and don't appear on the clients payers.

How can I solve that?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I solved the problem. I found that I should register the prefabs in the network manager registered spawnable prefabs in order to be spawned on all connected devices.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .