x


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

more ▼

asked Apr 07 '11 at 07:25 PM

Ignacio 1 gravatar image

Ignacio 1
56 8 8 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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.

more ▼

answered Apr 08 '11 at 02:35 PM

Ignacio 1 gravatar image

Ignacio 1
56 8 8 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1670
x1253
x374
x115

asked: Apr 07 '11 at 07:25 PM

Seen: 1350 times

Last Updated: Apr 07 '11 at 09:08 PM