Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Unity Multiplayer [SOLVED] Add prefab to "Registered Spawnable Prefabs" list via script not working well

Discussion in 'Connected Games' started by kigm, Jun 2, 2016.

  1. kigm


    May 13, 2016

    I am trying to add in NetworkManager a prefab in the Registered Spawnable Prefabs list, but when I add the prefab this appears like (clone), and in client side the prefab it isn't spawn and an error is throw.

    Here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    5. public class EmptyPlayer : NetworkBehaviour {
    7.     public GameObject playerObj;
    9.     public override void OnStartLocalPlayer()
    10.     {
    11.         if (isServer)
    12.         {
    13.             //empty player for showing nothing in client, just server's player
    14.             GameObject newPlayer = (GameObject)Instantiate(playerObj,
    15.                 transform.position, transform.rotation);
    17.             //add to Registered Spawnable list the resource for spawn in client
    18.             GameObject resource = Resources.Load ("Cube") as GameObject;
    19.             FindObjectOfType<NetworkManager>().spawnPrefabs.Add(resource.gameObject);
    21.             NetworkServer.ReplacePlayerForConnection(base.connectionToClient, newPlayer, 0);
    23.             NetworkServer.Destroy(gameObject); //This line of code will destroy Server's emptyPlayer after it's new playerObj is Instantiated.
    25.             //spawn this player in client
    26.             NetworkServer.Spawn (newPlayer);
    28.         }
    30.         base.OnStartLocalPlayer();
    31.     }
    33.     void Update()
    34.     {
    35.         if (isServer)
    36.         {
    37.             Debug.Log ("soy el server");
    38.             return; //This prevents the Server from reading updates while it's still an emptyPlayer.
    39.         }
    41.         if (isLocalPlayer)
    42.         {
    43.             //All of your spectators will use code here in-order to move around in the world and watch the Host.
    44.             Debug.Log ("soy el cliente");
    45.         }
    46.     }
    47. }
    And this is what happen:

    In NetworkManager if I add the prefab via script this is what it is show:

    And if I drag the prefab from Editor it is show like this:

    The last image represent the correct way spawning well in client, but if I add the prefab from script the first error is showed.

    Can anybody help me, please? I need attach models in runtime because user can upload and change models, then I cannot add models manually.
  2. kigm


    May 13, 2016
    Testing it... The problem is that in client side the Registered Spawnable prefabs is empty.
    I thought this list is shared between client and server.

    How can I do that?
  3. Oshroth


    Apr 28, 2014
    To spawn a prefab on the client it must be registered so that the client knows which prefab to spawn.
    The Registered Spawnable Prefabs list allows you to set from the editor what prefabs the clients will be able to spawn. When the client joins a server, it registers all of the prefabs in its list so changing it on the server won't affect the clients and clients changing it after they join the server won't affect it either.

    You need to use ClientScene.RegisterPrefab() to add extra spawnable prefabs after you've joined a server. In your case you need to either make Cube a proper prefab and add it to the spawnable list in the editor, or use a Spawn Handler to manually load it, since spawning it may not work even if you register it due to it being loaded as with Resource.

    This is the code used to register the prefabs when the client joins a server. The player prefab is also registered in the same way.
    Code (CSharp):
    1. foreach (var prefab in m_SpawnPrefabs)
    2.             {
    3.                 if (prefab != null)
    4.                 {
    5.                     ClientScene.RegisterPrefab(prefab);
    6.                 }
    7.             }
  4. kigm


    May 13, 2016
    Yes, this whas the problem, I had to register in Client scen as well.

    Thank you!
  5. terryyyyyyy


    Jun 19, 2017
    Hi all,

    I'm new to unity networking system. I am doing something similar stuff like kigm, where i wanna spawn a prefab at the Registered Spawnable Prefabs of Network Manager. I need attach prefab in runtime as it is generated only when the Kinect is on.
    I don't understand what should i replace with 'm_SpawnPrefabs'.
    Whatever i put , i will face the problem of "foreach statement cannot operate on variables of type 'UnityEngine.GameObject' because 'UnityEngine.GameObject' does not contain a public definition for 'GetEnumerator'."

    Can anybody help me, please?
  6. r1ft1


    Aug 12, 2017

    Did you figure it out?
  7. hexagonius


    Mar 26, 2013
    @terryyyyyyy m_SpawnPrefabs is either an Array of GameObjects, or a List. There's more types that define a GetEnumerator method, but these are the two that show up in the inspector