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

Could you please post the solution code ? Thanks !

@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.

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.