Allowing client players to access objects spawned only when the server player creates the server?

Hey guys!

So I am trying to create an object pooling system in Unity with uNet!

Basically when the host joins (via the LAN Host(H) button), it calls for initialization of the object pool. I instantiate the objects and when each object is spawned that object also gets a NetworkServer.Spawn() call.

All my objects have their Network Identity Components on them set to Local Player Authority as I assume that is what I need for this.

This is the method that does all of that:

[RPC]
private void RpcSpawnObjects(GameObject[] one, GameObject[,] two, string poolName)
    {
        poolCreated = 0;

        foreach (GameObject go in one)
        {
            //Creates a parent object for the pool of this particular object.
            GameObject newPool = (GameObject)Instantiate(new GameObject(), transform.position, Quaternion.identity);
            newPool.AddComponent<NetworkIdentity> ().localPlayerAuthority = true;
            NetworkServer.Spawn (newPool);
           
            //Names the pools and sets them as children to the ObjectPoolManager.
            newPool.name = poolName + poolCreated;
            newPool.transform.parent = this.transform;

            for (int o = 0; o < 50; o++)
            {
                //Creates the Objects for the Network.
                two [poolCreated, currentPooledItem] = (GameObject)Instantiate (one [poolCreated], new Vector3 (0, -10, 0), Quaternion.identity);
                NetworkServer.Spawn (two [poolCreated, currentPooledItem]);

                //Sets parent and deactivates object.
                two [poolCreated, currentPooledItem].transform.parent = newPool.transform;
                two [poolCreated, currentPooledItem].SetActive (false);

                currentPooledItem++;
            }

            currentPooledItem = 0;
            poolCreated++;
        }
    }

It seems to work fine for the Host. But what I need is that after the Host has created the server, any other player that joins the server can also has access to all these same objects.

Right now when I go into the server as a client, I don’t see any of the objects and of course it does not spawn new ones because I have this method only to get called when the server is created.

So what I’m asking is how can I make it so that the objects that are spawned when the Server is created are also already there as soon as a client joins the server?

Maybe I just need a new way of thinking about object pooling than what I have here…

If you guys need this cleared up at all, please let me know and I’ll get back to you ASAP. I’m just starting with uNet but would really like to figure this out as it would be a big step in my learning!

Thanks ahead of time!

  • Joe

You need to assign a asset id to the object and register it on the clients before you are able to spawn it. Typically you create a prefab and then assign it to NetworkManager->Registered Spawnable Prefabs. There are existing examples for Object Pooling in Unet around such as Networked object pool example for UNet HLAPI - Unity Engine - Unity Discussions