- Home /
Adding a NetworkView component at runtime
Hi! I'm trying to add a NetworkView component to an object at runtime. Let me give you some background first: I'm trying to share a camera's transform across the network. In order to do that, I created a prefab that contains a camera (and which doesn't own a NetworkView component), instantiate it with Network.Instantiate and add a NetworkView component at runtime. Here's the code:
Network.Instantiate(AstronautCamera, AstronautCamera.transform.position, AstronautCamera.transform.rotation, 0);
GameObject AstronautCameraInstance = GameObject.FindWithTag("AstronautCamera");
AstronautCameraInstance.AddComponent(typeof(NetworkView));
AstronautCameraInstance.networkView.observed = AstronautCameraInstance.transform;
AstronautCameraInstance.networkView.stateSynchronization = NetworkStateSynchronization.ReliableDeltaCompressed;
NetworkViewID viewID = Network.AllocateViewID();
AstronautCameraInstance.networkView.viewID = viewID;
The code above compiles and executes fine, but it's not doing what I expect it to do (i.e., when running in networked mode, the camera's transform doesn't get transferred across to the other hosts). Notice that assigning a NetworkView component to the prefab in the editor and instantiating the prefab using Network.Instantiate works fine, but I really need to do it by code.
Do you have any suggestions? Thanks in advance for your input!
--Nacho
Answer by Ignacio 1 · Apr 08, 2011 at 02:35 PM
I finally solved it! The problem was that I wasn't adding the NetworkView component inside an RPC. I made a few changes and now everything's working OK.
Answer by AjinkyaKshirsagar · Oct 16, 2013 at 03:08 AM
Could you please post the solution code ? Thanks !
Answer by emilhauk · Apr 29, 2014 at 08:59 PM
@AjinkyaKshirsagar: Late, but someone may need this.
This would be a way to attach a NetworkView runtime
public void Awake()
{
AddNetworkView();
}
[RPC]
private void AddNetworkView()
{
gameObject.AddComponent<NetworkView>();
gameObject.networkView.observed = this;
gameObject.networkView.stateSynchronization = NetworkStateSynchronization.ReliableDeltaCompressed;
gameObject.networkView.viewID = Network.AllocateViewID();
}
The trick is to add [RPC]. This will add the network view to the queue of messages to be shared with other players.
This doesn't really work, if you really wanted to do this runtime and not only during Awake, but somewhere else, it wouldn't happen on all clients, only that one you call it on, thus network view would not be operational. Only solution that works is to create network view always, but disable it when you don't need it
Your answer
Follow this Question
Related Questions
How to instantiate a Prefab ? 2 Answers
Adding behaviors to a game object at run time via scriptable object 1 Answer
nee help with Instantiating prefab at mouse position with GUI button 0 Answers
Instantiate a prefab at runtime 2 Answers
Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers