Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by darthbator · Aug 25, 2015 at 11:35 PM · networkingnetworkspawnchild object

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?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KaldrisRelm · Aug 27, 2015 at 08:36 PM 0
Share

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?

avatar image darthbator · Aug 27, 2015 at 08:44 PM 0
Share

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.

avatar image KaldrisRelm · Aug 27, 2015 at 10:10 PM 0
Share

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?

avatar image darthbator · Aug 27, 2015 at 10:36 PM 0
Share

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.

avatar image darthbator · Aug 30, 2015 at 11:12 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges