I’m creating a Terrain object which is procedurally generated with this script:
public GameObject terrain;
public override void OnStartServer(){
CmdCreateTerrain ();
}
[Command]
private void CmdCreateTerrain(){
GameObject tempObject = Instantiate (terrain, new Vector3 (0, -1, 0), Quaternion.identity) as GameObject;
tempObject.GetComponent<WorldGeneration> ().StartScript ();
NetworkServer.Spawn (tempObject);
}
This script is attached to a empty gameobject with a network identity and the terrain also has a network identity and network transform but when i start the server the terrain is created, then when i join with a client there’s no terrain and it just falls forever through a empty scene why isn’t this working?
I found a workaround i was hoping to synchronize the single map to all clients from the server, but i just created the same map on each client by copying the heights from the server terrain with [syncvar] to all clients terrain objects.
If your terrain is procedurally generated you could use System.Random with a seed int value which gets sent into a client RpcCreateTerrain(int seed) function… something like this:
override void OnStartServer()
{
RpcCreateTerrain(UnityEngine.Random.Range(0,100));
}
[RpcClient]
void RpcCreateTerrain(int seed)
{
System.Random r = new System.Random(seed);
terrain.SetHeight(0, 0, r.NextSingle());// ← will be the same for all clients
}
How do you syncvar the heights? It seems to be a 2 dimensional array, which I am not sure how to split up into single arrays, and then combine them again for the clients.
Thanks for the response but this is similar to what i did as a workaround i wanted to know if there is a way to synchronize one terrain object across all clients as it would make it much easier to use for other scripts which use or change the terrain.