- Home /
 
How to get NetworkViewID and GameObject attached to NetworkViewID
I can't seem to find reliable information about this.
I have a Network View component attached to a game object. How can I access this Game Objects NetworkViewID from code?
I have tried gameObject.networkView.viewID which seems to return a NetworkViewID (could someone please confirm this is the NetworkViewID?).
I then pass it to a RPC function as such:
 @RPC function mountCloud(netViewID : NetworkViewID) {
     
     /* TRYING TO IMPLEMENT THE FOLLOWING CODE, DONT KNOW HOW TO GET THE GAMEOBJECT FROM THE NETWORKVIEWID PASSED IN
     mount = true;
     mountObject = netViewID.gameObject; //////////////////////////////////////////
     mountObject.transform.parent = this.transform;
     mountObject.transform.localPosition = Vector3(0,0,0);
     mountObject.collider.enabled = false;
     setWalk(false);*/
     
     if (networkView.isMine)
         networkView.RPC("mountCloud", RPCMode.OthersBuffered, netViewID);
 
 }
 
               If this is the correct way to get the NetworkViewID, how can I get the gameObject from the NetworkViewID?
Answer by nastasache · Dec 19, 2013 at 10:39 PM
Yes, gameObject.networkView.viewID is a NetworkViewID type, so this is what you looking for.
(sorry all below in C#, I have no versions for JS but maybe will help you somehow)
I have in C# something like:
NetworkViewID targetViewID = hit.collider.gameObject.networkView.viewID;
(to find the NetworkViewID, in this case of an object hit by a bullet).
Example of reading/writing networkviews (object having multiple nw):
[RPC] void SpawnPlayer (NetworkViewID transformViewID) { instantiatedPlayer = Instantiate(playerPrefabs, spawnPoint, rotation) as Transform;
 NetworkView[] networkViews = instantiatedPlayer.GetComponents<NetworkView>(); // Read 
 
 Debug.Log(networkViews[0].viewID);
 
 networkViews[0].viewID = transformViewID; // Write
 
 Debug.Log(networkViews[0].viewID);
 
               }
If you have only a NetworkViewID value and want to find the object in hierarchy, can use:
GameObject FindObjByNetworkViewID(NetworkViewID lookingForViewID) {
 NetworkView view = NetworkView.Find(lookingForViewID);
 
 Debug.Log(view.observed.name);
     
 return view.gameObject;
 
               }
What a junk q&a site... I wish i could edit this post to make it readable.
Your answer