- Home /
How do I spawn inactive objects on new clients?
So it appears that if an object spawned with NetworkServer.Spawn is inactive on the server when a client connects, it will never be spawned on that client even if it is reactivated. Is this intended behavior? And how can i work around it, is my only option to never disable any object on the server?
have you tried spawning the object as active and then triggering it to be inactive?
This could also be a bug which may be worth infor$$anonymous$$g unity about, could you also note down what version of unity you are using, have you got some code example we could see to ensure the intended behaviour is what you think it is doing. assu$$anonymous$$g you are using UNet.
Answer by firsy · Apr 08, 2017 at 06:50 AM
So i delved through the source code to see where exactly the distinction between inactive and active objects happens when spawning, and i found it in NetworkServer.SetClientReadyInternal, this is the part that is called only for connections that aren't the local host:
foreach (NetworkIdentity uv in objects.Values)
{
if (uv == null)
{
if (LogFilter.logWarn) { Debug.LogWarning("Invalid object found in server local object list (null NetworkIdentity)."); }
continue;
}
if (!uv.gameObject.activeSelf)
{
continue;
}
if (LogFilter.logDebug) { Debug.Log("Sending spawn message for current server objects name='" + uv.gameObject.name + "' netId=" + uv.netId); }
var vis = uv.OnCheckObserver(conn);
if (vis)
{
uv.AddObserver(conn);
}
}
Basically when you call NetworkServer.SetClientReady(conn), conn is only added as an observer to objects that have activeSelf == true, unless its the local connection. So we can just add the connection as an observer to the objects that are inactive, right after calling NetworkServer.SetClientReady. Heres the code im using:
private void SetClientReadyIncludeInactive(NetworkConnection conn) {
NetworkServer.SetClientReady(conn);
//tell client to spawn inactive objects, because SetClientReady wont
AddObserverToInactive(conn);
}
private void AddObserverToInactive(NetworkConnection conn) {
if(conn.connectionId == 0) {
//local host has observer added to inactive NetworkBehaviours by SetClientReady already
return;
}
foreach(NetworkIdentity netId in NetworkServer.objects.Values) {
if(netId == null) {
Debug.LogError("Trying to add observer to object of null NetworkIdentity.", this);
continue;
}
if(!netId.gameObject.activeSelf) {
MethodInfo OnCheckObserver = typeof(NetworkIdentity).GetMethod("OnCheckObserver", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo AddObserver = typeof(NetworkIdentity).GetMethod("AddObserver", BindingFlags.NonPublic | BindingFlags.Instance);
if((bool)OnCheckObserver.Invoke(netId, new object[] { conn })) {
AddObserver.Invoke(netId, new object[] { conn });
}
}
}
}
The only thing to worry about now is that objects will be spawned as active on the new client, but I could just use a sync var for that. Or maybe send a message to the new client with a list of all the inactive objects and then set them as inactive in the handler.
Edit: Still not sure if this is a bug or not, if not i think SetClientReady should take a bool that says whether to add the connection as an observer to inactive objects. Maybe I'll add a pull request if this approach works out well.
firsy what did you end up doing in the end? Gather a list of inactives and send to client?
Where did you run SetClientReadyIncludeInactive()?
Where did you call SetClientReadyIncludeInactive()?
Answer by PigBang · Apr 04, 2018 at 09:58 AM
We has this problem for our cached objects. They disabled when in cache. For us help call NetworkServer.Spawn() after each reuse object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612114817im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[UNET] How to know the connectionID of a player ? 2 Answers
No appropriate version of 'UnityEngine.Object.Instantiate' 0 Answers
How to spawn gameobject from host to all clients 1 Answer
UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer
UNET NetworkServer.Spawn intermittently not called on clients 1 Answer