- Home /
Unity Network
I'm using the unity networking system. I have 2 cars and a police light. I can turn my own police light on without turning theirs on, but I can't see it in the other screen.
For example, My friend is on his comp, and I'm on mine. I turn my lights on, but he can't see it. What did I do wrong? Here is my code.
var on : KeyCode;
var off :KeyCode;
function Update () {
if (networkView.isMine)
if(Input.GetKeyDown(on))
GetComponent(Light).enabled = true;
if (networkView.isMine)
if(Input.GetKeyDown(off))
GetComponent(Light).enabled = false;
}
Thanks, Tim
Answer by Bunny83 · Dec 02, 2012 at 02:43 AM
No state is transferred automatically to other peers. A NetworkView is responsible vor sync-ing certain informations. The component specified in the observed variable is getting it's public variables serialized and transfered over the net. You need one NetworkView for each component you want to synchronise. An alternative is to observe your script component and implement the OnSerializeNetworkView function. There you can manually specify what data you want transmit.
Note: If you don't implement the OnSerializeNetworkView function, Unity will serialize the public variables of your script when it's observed by a NetworkView.
So one solution should be to add another networkview and drag your light component onto the observed variable. That way all properties should be transferred.
edit
I've just done some tests and it seems Unity only supports a few components to be observed by a NetworkView. A Light component can't be observed (which is a pity, the concept is not bad, but unfortunately they don't stick to it).
So since automatic-synchronising doesn't work with a Light component you have to use OnSerializeNetworkView and let a NetworkView observe your script.
I've created a sample project (including source) which contains the following things:
Initializing a server / registrating the server at the MasterServer
Connecting to a server (direct connect via IP or via MasterServer game list)
Simple chat system
Instantiating player objects and synchronising position / velocity of the players rigidbody
Syncronising other states like the light status.
Sample usage of RPCs
Using a texture as color picker
And finally here is a standalone build of the "game" and like always a webbuild (requires the Unity WebPlayer)
This project was done in Unity 3.5.0
Thanks! I'm still a little confused. so I make a script that has an observed variable and what function and what in the script
Thanks! Can you save them as .zip not .rar please. I can't open them.
Answer by TheDarkVoid · Dec 02, 2012 at 03:13 AM
You could also use `networkView.RPC()` to send the data to the peers. This is useful if you only want to send small amounts of data periodicity. Try not the send RPCs every frame, just on a change.
So, I add a RPC to the lights? How would I do it? Sorry, I wasn't clear.
You don't add a RPC to the lights. You add a NetworkView to your lights, and use RPC calls to tell all the other users to turn the light on/off.
OrangeLightning, I have a network view, but I dont know how to call a RPC on them.
Answer by zabos · Apr 22, 2013 at 11:45 PM
can you tell me how i can switch on/off the lights with key button insted of gui button?thnx man