- Home /
Question by
khalidhex · Dec 01, 2016 at 10:13 PM ·
c#networkingmultiplayermultiplayer-networking
send data from client to local server
Hello ..
I'm new to unity networking and I'm trying to transform my single player card game to multiplayer........ basically i have two scenes client and server and I'm trying to send the (card name) that the client throw to the server ........... but it's return Null when already there is a card in the throw zone .
#pragma warning disable 618
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class MainNetwork : NetworkBehaviour
{
public string IP = "127.0.0.1";
public int PORT = 25001;
void OnGUI()
{
if (Network.peerType == NetworkPeerType.Disconnected)
{
if (GUI.Button(new Rect(100, 100, 100, 25), " Start Client"))
{
Network.Connect(IP, PORT);
}
if (GUI.Button(new Rect(100, 125, 100, 25), " Start Server"))
{
Network.InitializeServer(3, PORT, true);
}
}
else
{
if (Network.peerType == NetworkPeerType.Client)
{
GUI.Label(new Rect(100, 100, 100, 25), " Client Loged");
if (GUI.Button(new Rect(100, 150, 100, 25), "Log Out"))
{
Network.Disconnect();
}
if (GUI.Button(new Rect(100, 190, 100, 25), "Thrown Card Name"))
{
GetComponent<NetworkView>().RPC("run", RPCMode.Server);
}
}
if (Network.peerType == NetworkPeerType.Server)
{
GUI.Label(new Rect(100, 100, 100, 25), " Server Started");
GUI.Label(new Rect(100, 125, 100, 25), "" + Network.connections.Length);
if (GUI.Button(new Rect(100, 150, 100, 25), "Log Out"))
{
Network.Disconnect();
}
}
}
}
[Client]
public string ClientThrownCardName()
{
var throwzone = GameObject.Find("ThrowZone");
return throwzone.transform.GetChild(0).transform.name;
}
[RPC]
public void run()
{
Debug.Log(ClientThrownCardName());
}
Comment