Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Hello,

    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;
    4.  
    5. public class EmptyPlayer : NetworkBehaviour {
    6.  
    7.     public GameObject playerObj;
    8.  
    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);
    16.  
    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);
    20.  
    21.             NetworkServer.ReplacePlayerForConnection(base.connectionToClient, newPlayer, 0);
    22.  
    23.             NetworkServer.Destroy(gameObject); //This line of code will destroy Server's emptyPlayer after it's new playerObj is Instantiated.
    24.  
    25.             //spawn this player in client
    26.             NetworkServer.Spawn (newPlayer);
    27.  
    28.         }
    29.  
    30.         base.OnStartLocalPlayer();
    31.     }
    32.  
    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.         }
    40.  
    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

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    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

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    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

    kigm

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

    Thank you!
     
  5. terryyyyyyy

    terryyyyyyy

    Joined:
    Jun 19, 2017
    Posts:
    1
    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

    r1ft1

    Joined:
    Aug 12, 2017
    Posts:
    1

    Did you figure it out?
     
  7. hexagonius

    hexagonius

    Joined:
    Mar 26, 2013
    Posts:
    95
    @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