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.
- A server connects to the game
- In his start method he calls a function
CmdbuildEverything
to spawn some objects to the game. - 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?