- Home /
UNET Network.Spawn and child objects
Hey there everyone! I'm wondering how I am supposed to go about dealing with child objects in the new unet networking system?
For example my base player object is a series of nested objects. Using the standard HLAPI unet NetworkManager when I instantiate my player only the top level object appears to actually be Spawned on the network. All the objects displays appear to be there but the NetworkId component does not appear to be initialized. If I attempt to spawn that object it appears to spawn a completely new instance of the entire nested player prefab object. How am I supposed to do this?
I have tried having the parent object iterate over the child list OnLocalPlayer start and then spawn every child object but I just get a heap of player objects! I have tried having each child spawn itself on Start but it also has the same issue.
How am I supposed to go about correctly spawning a nested tree of objects over the network?
What are these child objects used for, are they controlled by the parent object or do they need their own tracking for things like position?
They currently have their own network transforms and identities. I suppose I could use the parent object to do all that logic...
I would sort of prefer not to if at all possible. However if I'm going down a dark path by severely not the number of network tracked objects per player character in HLAPI I would love to know now so that I could start refactoring this code to give every character object a "child manager" type script.
Sounds like your setup is similar to a setup I've built using UNET. Short answer is no, there isn't, at least not that I've found, an easy way to spawn child objects like you've mentioned, but I've built a workaround that should be able to help you get this working. Would you be open to showing your scripts / project so I can get a better idea of what exactly you need?
If I am having troubles a little later still sure! I'll probably send you a P$$anonymous$$ or something tonight if I'm still stuck on this but I've been making some progress! What appears to be happening by default is the child objects are all being spawned outside of tracking by the spawn system with uninitialized NetworkIdentities as part of the general player object. I guess for the sake of performance it would be bad to just assume you wanted to track every child of a network tracked object over the network.
So I can get a mostly functional version of this working by having each players object spawn it's children via command (which is fine for my game). The issue I am really running into is that the object hierarchy on the clients does not appear to reflect that of the server. Here's a basic example where I want to just hand a player a debug weapon (in this case a gun). The flow here is the local player sends a command that requests a gun from the server side item dispenser script. The item dispenser then hands back the gun and the player object spawns the gun after it's finished setting it up on the server. Here's how the code looks
//FRO$$anonymous$$ PLAYER.CS
[Command] public virtual void CmdRequestDebugGun () {
Item debugGun = ItemDispenser.Instance.DispenseDebugGun();
debugGun.transform.position = transform.position;
debugGun.wielder = this;
debugGun.transform.SetParent(transform);
NetworkServer.Spawn(debugGun.gameObject);
}
//FRO$$anonymous$$ ITE$$anonymous$$DISPENSER.CS
[Server] public Item DispenseDebugGun () {
Item newItem = Instantiate(DebugGun, transform.position, Quaternion.identity) as Item;
return newItem;
}
Pretty much works as you would expect! However the parenting that I have set on the host(server) does not appear to be reflected on the clients. Do I need to catch the spawn on each local client and assign the child there?
That actually wouldn't even be a big deal but I need to find a way to deter$$anonymous$$e who spawned the object so that I can correctly child it on the client instances of the game. Does the network transform or networkIdentity have any concept of "player who spawned me". Something like that could probably fix this up for me pretty quickly.
Your solution actually worked well for me. It would be ideal if Unity did it's best to preserve hierarchy between clients and servers. At least in cases where a network object is childed to another network tracked object. If I set something to be a parent on the server why would I want it to not reflect that on a client...
You should put that in as an answer! It fixed my issue. It's also among the only types of work around for this issue until the engine itself does something to track hierarchy during spawning.
Answer by KaldrisRelm · Aug 27, 2015 at 11:15 PM
In my project syncing the hierarchy is incredibly important, so maybe my setup will help. All of my hierarchy is based off of tags. Every object that can be spawned for a player contains 2 empty game objects named 'PlayerTag' & 'TeamTag'.
The tags set on these two objects are used to mark for every other player what is a friend, enemy or their own object. The main Player Prefab is also marked with the same PlayerTag, so using a 'GameObject.FindGameObjectWithTag ("PlayerOne");' will always return the proper player.
From here I use a SyncVar and a Hook to allow clients to properly sort new Gameobjects in the hierarchy after they are spawned:
[SyncVar(hook = "SetPlayerTag")] public string PlayerTag;
public void SetPlayerTag(string NewTag)
{
transform.name = "Carrier";
transform.FindChild ("PlayerTag").tag = NewTag;
transform.SetParent (GameObject.FindGameObjectWithTag (NewTag).transform);
}
Considering your CmdRequestDebugGun() function, using my above example I would have the player calling the command pass their PlayerTag as a string variable. That'll let your server spawn the object, then set the PlayerTag to the synced string. Since SyncVar is called by all clients as well as the server any code you put in should automatically sync hierarchies, In my case I sync hierarchy, names & tags.
Your answer
Follow this Question
Related Questions
How to network spawn an object from a downloaded asset bundle 0 Answers
network card game 2 Answers
zombie network spawn! help" 0 Answers
UNET save server (world) progress 1 Answer
UNET NetworkServer.Spawn intermittently not called on clients 1 Answer