- Home /
Can NetworkView.owner be set?
Is it possible to transfer "ownership" of a network view to different players over time?
Answer by AKAssassin · Apr 14, 2011 at 05:26 PM
Kinda, but not in a very nice and direct way. From my understanding of Unity networking so far where ever you allocate the networkview.viedID is who the owner is.
For example is you instantiate an object that has a networkview on a client and then on that client says,
var id = Network.AllocateViewID();
obj.networkView.viedID = id;
then the client will be the networkView.owner, if you were to do that on the server, the server would be the owner.
So...
If you want to transfer ownership of an object it should be possible to do by allocating a new view id for the object on whoever you want to own the object and then assign the new id to the existing networkview, and the owner should now be changed.
But you would want to allocate the id first then send an rpc to all the other versions of the object on remote computers to change the id and then set the id. This way you don't break the viewid relationship across all computers running the game.
Hopefully this helps, I haven't actually tried doing this myself but if it is possible to do want you want to do, this is probably the most likely way of doing it.
Thanks so much, that seems entirely accurate to me! :) I have not yet tested, but what you say Should work :D
I've just tried this out this week and I can confirm that it does indeed work as expected. It brought up a couple of issues that I hadn't anticipated, though:
Firstly, changing the view ID messes up any buffered RPCs that were sent via the old ID. They still get sent, but a newly connecting client can't find the NetworkView and gives an error. I worked around that by having an extra NetworkView on my objects which I just use for RPCs, and that NetworkView is forever owned by the server and never gets its ID changed.
Secondly, if you send the RPCs to change IDs from a client, the server can sometimes send an additional state change from the old ID and clients receive that after they've changed to the new one, and that also gives an error on the clients when they can't find the NetworkView. The solution to that was to have the server change the IDs on its own NetworkViews, and then send another RPC to all the clients telling them to change their IDs. So in this setup the new owner sends a RPC$$anonymous$$ode.Server RPC with the new ID, and then the server does its own RPC$$anonymous$$ode.AllBuffered RPC to relay that to all the clients.
@JJC1138 - I wish I could upvote your comment! This was very useful information and gave me some insights on what I can do with my own networking code to transfer ownership in addition to what A$$anonymous$$Assassin posted in his answer. Thanks for sharing :)
I also feel I should mention that this same kind of technique can be used with Photon Unity Networking.
@jjc1138 Could you show me your code? This thing is very important and I can't figure how to do it..
Answer by Baumkuchen · Jul 30, 2016 at 04:34 PM
now there is a solution for photonengine: https://doc.photonengine.com/en/pun/current/tutorials/ownership-transfer
Or you simply call "photonView.RequestOwnership();" on that object witch owner should be changed
Your answer
![](https://koobas.hobune.stream/wayback/20220613095818im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get NetworkPlayer from ViewID 1 Answer
Photon - How to name a player? 0 Answers
networkview.RPC not accessible in unity 5.4 1 Answer
Client can't serialize to client 1 Answer