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

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:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class EmptyPlayer : NetworkBehaviour {

    public GameObject playerObj;

    public override void OnStartLocalPlayer()
    {
        if (isServer)
        {
            //empty player for showing nothing in client, just server's player
            GameObject newPlayer = (GameObject)Instantiate(playerObj,
                transform.position, transform.rotation);

            //add to Registered Spawnable list the resource for spawn in client
            GameObject resource = Resources.Load ("Cube") as GameObject;
            FindObjectOfType<NetworkManager>().spawnPrefabs.Add(resource.gameObject);

            NetworkServer.ReplacePlayerForConnection(base.connectionToClient, newPlayer, 0);

            NetworkServer.Destroy(gameObject); //This line of code will destroy Server's emptyPlayer after it's new playerObj is Instantiated.

            //spawn this player in client
            NetworkServer.Spawn (newPlayer);

        }

        base.OnStartLocalPlayer();
    }

    void Update()
    {
        if (isServer)
        {
            Debug.Log ("soy el server");
            return; //This prevents the Server from reading updates while it's still an emptyPlayer.
        }

        if (isLocalPlayer)
        {
            //All of your spectators will use code here in-order to move around in the world and watch the Host.
            Debug.Log ("soy el cliente");
        }
    }
}

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.

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?

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.

foreach (var prefab in m_SpawnPrefabs)
            {
                if (prefab != null)
                {
                    ClientScene.RegisterPrefab(prefab);
                }
            }

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

Thank you!

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?

Did you figure it out?

@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