How can I call a function when all NetworkObjects are spawned on a client?

Whenever a client joins, I’m trying to have an in-scene-placed network object access data from each player object for them. When the function is called using OnNetworkSpawn(), not all player objects are spawned in for that client yet and I can’t find a way to call the function when they all are.

public override void OnNetworkSpawn()
    {
        if(IsClient)
        {
            foreach(GameObject newObj in GameObject.FindGameObjectsWithTag("PM"))
            {
                Debug.Log("Found object.");
            }
        }
    }

The above code only logs once on the client despite there being more network objects with the tag “PM” in the scene. Thank you :slight_smile:

Hi @RattusMakeGame , if the server knows when all the players are connected, it can send a ClientRPC so all players do what they need to do

I should have mentioned that the goal was to know when all player objects are spawned on the client-side even when joining in the middle of a session. Luckily, I found a solution but I appreciate the help :slight_smile:

The solution basically involved having the server hold a count of all players and having the client wait to make sure the count was greater than 0, then count all the player objects repeatedly in update using a tag and FindGameObjectsWithTag and execute the code once when they are equal.

If there is a more efficient way to do this I would be glad to hear, but it works well enough for now.

2 Likes

Would you mind sharing your wisdom? I a m having the same issue of injecting config into client GameObjects when they enter the scene, but OnNetworkSpawn doens’t ensure that the objets exists in all clients, only that the local game object spawned.

Even I am facing the same issue, I want to parent network objects as soon as they spawn, But they only get parented in Host not in clients because objects are not spawned on all clients, I don’t know a way to wait for that on the host