Search Unity

Puzzling Simple Prefab Instantiate Behavior

Discussion in 'Multiplayer' started by Obsolescence, Jul 29, 2017.

  1. Obsolescence

    Obsolescence

    Joined:
    Feb 19, 2017
    Posts:
    11
    Hi everyone, I'm new to Unity networking code. I'm just trying to get a prefab to spawn for all clients. My code is below. This script is attached to an empty gameObject in the scene. The issue I'm having is that the "baby" prefab is only spawning for the host, but not the connecting client. I'm running on localhost (Run and Build [Host] + Editor Play [Client]). Line #33 and #34 never seem to run and I don't get a "Here3" message in the console (line #33). Line #26 appears to correctly spawn a baby but only for the hosting client, not the connecting client. Am I missing something simple? Any help is appreciated. The Network Identity component is not set to "Server Only" or "Local Player Authority", both unchecked.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class BabyMaker : NetworkBehaviour {
    8.  
    9.     public GameObject baby;
    10.  
    11.     void Start ()
    12.     {
    13.         Debug.Log("Here1");
    14.         CmdbabyMaker(new Vector3(0, 2, 0));
    15.     }
    16.  
    17.     void Update ()
    18.     {
    19.      
    20.     }
    21.  
    22.     [Command]
    23.     public void CmdbabyMaker(Vector3 pos)
    24.     {
    25.         Debug.Log("Here2");
    26.         Instantiate(baby, new Vector3(0, 2, 0), transform.rotation);
    27.         RpcbabyMaker();
    28.     }
    29.  
    30.     [ClientRpc]
    31.     public void RpcbabyMaker()
    32.     {
    33.         Debug.Log("Here3");
    34.         Instantiate(baby, new Vector3(0, 2, 0), transform.rotation);
    35.     }
    36. }
     
  2. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    hey, so the problem might be that you're calling your Command in the Start function.

    there are other functions to use for NetworkBehaviour like OnStartServer, OnStartClient, OnStartLocalPlayer which may help you here.

    you may also be running into problems where this code is running before a client has connected. as a quick test, you could try calling your command on the server when a key is pressed (after a client has definitely connected)

    & finally...if you intend to have copies of this 'baby' object on the server and all clients, you may want to give the baby prefab a networkIdentity component, and instantiate it with either NetworkServer.Spawn or NetworkServer.SpawnWithClientAuthority. this does a bunch of automagical stuff for you to make sure you don't need to spawn it in two different places! btw if you use the Spawn methods, you need to make sure the prefab is also included in NetworkManager's list of "Registered Spawnable Prefabs"
     
    TwoTen likes this.