Search Unity

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

Discussion in 'Multiplayer' started by Josepht45, May 30, 2016.

  1. Josepht45

    Josepht45

    Joined:
    Oct 20, 2014
    Posts:
    13
    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:


    Code (CSharp):
    1. [RPC]
    2. private void RpcSpawnObjects(GameObject[] one, GameObject[,] two, string poolName)
    3.     {
    4.         poolCreated = 0;
    5.  
    6.         foreach (GameObject go in one)
    7.         {
    8.             //Creates a parent object for the pool of this particular object.
    9.             GameObject newPool = (GameObject)Instantiate(new GameObject(), transform.position, Quaternion.identity);
    10.             newPool.AddComponent<NetworkIdentity> ().localPlayerAuthority = true;
    11.             NetworkServer.Spawn (newPool);
    12.            
    13.             //Names the pools and sets them as children to the ObjectPoolManager.
    14.             newPool.name = poolName + poolCreated;
    15.             newPool.transform.parent = this.transform;
    16.  
    17.             for (int o = 0; o < 50; o++)
    18.             {
    19.                 //Creates the Objects for the Network.
    20.                 two [poolCreated, currentPooledItem] = (GameObject)Instantiate (one [poolCreated], new Vector3 (0, -10, 0), Quaternion.identity);
    21.                 NetworkServer.Spawn (two [poolCreated, currentPooledItem]);
    22.  
    23.                 //Sets parent and deactivates object.
    24.                 two [poolCreated, currentPooledItem].transform.parent = newPool.transform;
    25.                 two [poolCreated, currentPooledItem].SetActive (false);
    26.  
    27.                 currentPooledItem++;
    28.             }
    29.  
    30.             currentPooledItem = 0;
    31.             poolCreated++;
    32.         }
    33.     }
    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
     
    Last edited: May 30, 2016
  2. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99