1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Unity Multiplayer Instantiating the terrain on the server on the client when the client connects

Discussion in 'Multiplayer Networking' started by gamer4717, Mar 18, 2017.

  1. gamer4717

    gamer4717

    Joined:
    Jun 25, 2014
    Posts:
    13
    Hi, I have a scene that gets an array of terrain prefabs located in the resources folder, converts the names of the terrains to a list, and stores them in a drop-down as well as an okay button that takes the value in the drop-down and jumps to another scene and spawns in the terrain. How can I get that value of that terrain prefab that is spawned in on the server to the clients that connect and Instantiate it in the scene at the same location and rotation it is on the server? I don't need the terrain to be a networked object, but the client does need to know which terrain to spawn in.

    As you may or may not be able to tell already, I am not that good when it comes to networking :)
     
  2. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    142
    well making the terrain be a networked object would do it for you automatically. of course you'd need to add it to the list of spawnable objects in the NetworkManager which is a little tedious to maintain

    another approach: when the server detects a player has joined, send that player a message with your terrain data.

    so in your player script, you'll want to override the function OnStartServer, and check if (!isLocalPlayer). if it's not the local player, you'll want to call something like connectionToClient.Send(msgType, msg) to send a message to that newly connected player with your terrain info.

    if you're not sure how to send a network message, have a look at this doc - https://docs.unity3d.com/Manual/UNetMessages.html

    basically you create your own message class that extends MessageBase, and you give it the properties you need - terrainId, position, rotation. the client starts listening for the message in OnStartClient, and the server sends the message to the client as i suggested above.
     
  3. gamer4717

    gamer4717

    Joined:
    Jun 25, 2014
    Posts:
    13
    @robochase thanks for the reply, but what I ended up doing was syncing the name of the terrain and on client connect, I used resources.load using the name to get the terrain that was running on the server and instantiated it locally.