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
0
Question by thedude22 · Aug 24, 2016 at 10:42 AM · networkingspawninginstancereference-other-object

How to properly setup my networked game objects

I am making a multiplayer board game. Part of the game is spawning units on the board controlled by a player, and so the player must have authority to issue commands on these non-player objects. I understand that I should use a command function and NetworkServer.SpawnWithClientAuthority to achieve this. In the command, I call a method to initialize a pair of integer SyncVars on the unit's script. Then I make an RPC call to send the unit gameobject so that the game on the client machine can access this script instance and these integers. However I am getting 0 for both, when they are not 0 on the server. So it seems passing the gameobject from the server command through a RPC does not result in grabbing the correct gameobject on the client's game.

I tried passing the networkInstanceId instead and using ClientScene.FindLocalObject to grab the script component, but it gives me a null reference exception.

Why is this happening? Is this not the proper way to grab a reference to this type of spawned object?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Aug 25, 2016 at 01:23 AM

I got lost in your description, so I'll tell you how I'd do it:

  • The client calls a Command method from it's Player object, telling the server whatever it needs to spawn.

  • The server instantiates the prefab of the unit and sets any value needed as a SyncVar

  • The server calls SpawnWithClientAuthority

  • Each client will recieve a message to instantiate a unit with the values of the SyncVar

  • When that unit is created the OnStartClient method of any NetworkBehaviour in that unit will be executed in all clients

  • Since the unit was spawned with client authority, the instance of the client that has authority will also execute the OnStartAuthority method on any NetworkBehaviour in that unit.

You don't have to tell the clients with an RPC what object was spawned, they're told by the networking system what to create, and the OnStartClient and OnStartAuthority methods are there for the clients to react with custom code to that event. If you do the Spawn in the server and immediatelly call an RPC with the uint reference, the clients will probably execute the RPC before they had time to process the "spawn" message received.

Check "Object creation flow" and "Spawning Objects with local authority" in the manual for more details: https://docs.unity3d.com/Manual/UNetSpawning.html

Comment
Add comment · Show 6 · 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
avatar image thedude22 · Aug 25, 2016 at 11:48 AM 0
Share

Any info on the proper way to grab references to server spawned objects would be greatly appreciated. I have spent a long time looking through the UNet api and searching forums and I can't figure it out. I also tried writing a custom spawn function according to the example in the documentation, but it wasn't being called for whatever reason. I just don't understand.

avatar image DiegoSLTS thedude22 · Aug 25, 2016 at 11:59 AM 0
Share

FindLocalObject is ok, but will work only after the object is synched. If you make sure the object has been spawned on a client it works. In your question it sounds like you're trying to find the object too soon. Anyway, if you pass the gameObject reference unet will do that for you.

avatar image thedude22 · Aug 25, 2016 at 05:01 PM 0
Share

Thank you very much for this explanation, but I am still a little confused. I have read the docs many times. It's just not clicking for me. I see that OnStartClient and OnStartAuthority will be called on the unit, but as far as I can tell they do not give any information about which player object spawned the unit object. This is what I need as it is the player object that will control the movement of all his/her units. The player object has a List of Unit objects. I don't know the proper way to fill this list. I guess I could loop over all player objects and look for which one has authority and send it the unit reference. I just thought there would be a better way to do it.

avatar image DiegoSLTS thedude22 · Aug 25, 2016 at 05:09 PM 0
Share

I don't know any method to just get a reference to the local player, but you can search for player objects (with FindObjectsOfType) and see how has isLocalPlayer set to true. Or you can have one reference in some place of easy access to "the local player", and make the local player on each client set te reference itself, then just get that reference on OnStartAuthority from the unit.

If you want all clients to be aware of the player who ownes each unit, maybe you should have a SyncVar on the unit to identify the owner. The server will set that variable and it'll be synched automatically for everyone, no need to search or notify them with another call.

avatar image Fattie thedude22 · Aug 10, 2018 at 09:57 AM 0
Share

theDude, the answer is simple,

you see in Diego's answer, point 3 ....

"The server instantiates the prefab of the unit and sets any value needed as a SyncVar"

at that point, the server would add the sort of information you mean, which is relevant to your game. for example ... "This item was created by ...", and so on.

Hope it helps!

avatar image mattseaton22 Fattie · Aug 10, 2018 at 03:18 PM 0
Share

Wow. Old thread. I think my problem was with the order of callbacks. I was assu$$anonymous$$g the callbacks were being called before they actually were and so my values weren't being initialized when I thought they were.

I have since abandoned any ideas of multiplayer since it's just too complicated for a lone dev like me working in my spare time. I now just focus on single player ideas. I see now that both the HLAPI and LLAPI are being deprecated also. I hope they come up with a better HLAPI in the future.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best method for handling references to objects on Clients from a Network.Spawn? 0 Answers

Multiple world spaces? 0 Answers

Networking | Getting buffered other - previous spawned players 1 Answer

Networking: How to spawn dynamically created objects 2 Answers

Network.Spawn. Not Spawned on clients except for host. 0 Answers


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