Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Network RPC send value from client to server

Discussion in 'Multiplayer' started by do1fen_f1, Jun 4, 2015.

  1. do1fen_f1

    do1fen_f1

    Joined:
    Apr 21, 2015
    Posts:
    6
    Hi,

    So I am trying to send data from client to the server, connection is done (client able to connect to the server) I know this by see the statistic, it say network is connected. So I want to try send value from client to server, I create a button at client (when it clicked, color sphere in the server changed). Unfortunately there are errors in servers side:

    View ID SceneID: 2 Level Prefix: 0 not found during lookup. Strange behaviour may occur
    Could't invoke RPC function 'changeColor' because the networkView 'SceneID: 2 Level Prefix: 0' doesn't exist

    Can someone help me?
    I am using two computers to try with unity version 5

    The code as below:

    using UnityEngine;
    using System.Collections;

    public class ServerClient : MonoBehaviour {

    public string connectionIP = "140.125.32.147";
    public int connectionPort = 23466;
    public GameObject target;

    void OnGUI()
    {
    if (Network.peerType == NetworkPeerType.Disconnected)
    {
    GUI.Label(new Rect(10, 10, 300, 20), "Status: Disconnected");
    if (GUI.Button(new Rect(10, 30, 120, 20), "Client Connect"))
    {
    Network.Connect(connectionIP, connectionPort);
    }
    if (GUI.Button(new Rect(10, 50, 120, 20), "Initialize Server"))
    {
    Network.InitializeServer(32, connectionPort, false);
    }
    }
    else if (Network.peerType == NetworkPeerType.Client)
    {
    GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Client");
    if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
    {
    Network.Disconnect(200);
    }
    if (GUI.Button(new Rect(10, 100, 120, 20), "Change COlor")){
    GetComponent<NetworkView>().RPC("changeColor",RPCMode.All,null);
    }
    }
    else if (Network.peerType == NetworkPeerType.Server)
    {
    GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Server");
    if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
    {
    Network.Disconnect(200);
    }
    }
    }

    [RPC]
    public void changeColor(){
    target.GetComponent<Renderer>().material.color = Color.green;
    }
    }
     
  2. erenaydin

    erenaydin

    Joined:
    Mar 1, 2011
    Posts:
    381
    Add a NetworkView component to server side, on the gameObject holds that script.
     
  3. do1fen_f1

    do1fen_f1

    Joined:
    Apr 21, 2015
    Posts:
    6
    Thanks for the reply bisaniyehocam. I did add NetworkView to both side on gameobject that hold the script. The error occur, I don't know why. Any others possible mistake that I did?
     
  4. Crystalline

    Crystalline

    Joined:
    Sep 11, 2013
    Posts:
    160
    RPC.mode should be MasterClient not all btw.
    Make sure the objects have the same net viewID, and be sure the server object has a script with the RPC function.
     
  5. do1fen_f1

    do1fen_f1

    Joined:
    Apr 21, 2015
    Posts:
    6
    Thank Crystalline for the reply. I noticed that the object viewID in server and in the client are different, in server object has viewID 1 and client has viewID 2. The problem is how to make them have the same viewID?
     
  6. Crystalline

    Crystalline

    Joined:
    Sep 11, 2013
    Posts:
    160
    Well thats your problem! I dont know how you spawn the objects. But you can allocate a view id on the server ,then rpc the client with server view id as a parameter, and assign the server view id to the client too.
     
  7. do1fen_f1

    do1fen_f1

    Joined:
    Apr 21, 2015
    Posts:
    6
    So actually can I use rpc to update variable value in server triggered by activity in client side? for example I have variable "point" in server side, then when object ball in client side collide with the floor, the value of variable "point" in server side will be increase by 1. Sorry I am still not really understand of how to use rpc. I don't really need to sync the objects, the object in server and client are different, I just want to send value from client to server.
     
    Last edited: Jun 9, 2015
  8. do1fen_f1

    do1fen_f1

    Joined:
    Apr 21, 2015
    Posts:
    6
    Hi,
    Anyone can explain to me actually how to set both side object at server and client has the same viewID?
    I used the sample in http://docs.unity3d.com/ScriptReference/NetworkView.RPC.html.
    I don't know why suddenly it could set the same viewID after many trying. But when I re-do it again, I couldn't set the same viewID.

    so here i my code:

    This code is attached at empty gameobject in both server and client:

    using UnityEngine;
    using System.Collections;

    public class ServerClient : MonoBehaviour {

    public string connectionIP = "140.125.32.147";
    public int connectionPort = 23466;


    void OnGUI()
    {
    if (Network.peerType == NetworkPeerType.Disconnected)
    {
    GUI.Label(new Rect(10, 10, 300, 20), "Status: Disconnected");
    if (GUI.Button(new Rect(10, 30, 120, 20), "Client Connect"))
    {
    Network.Connect(connectionIP, connectionPort);
    }
    if (GUI.Button(new Rect(10, 50, 120, 20), "Initialize Server"))
    {
    Network.InitializeServer(32, connectionPort, false);
    }
    }
    else if (Network.peerType == NetworkPeerType.Client)
    {
    GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Client");
    if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
    {
    Network.Disconnect(200);
    }

    }
    else if (Network.peerType == NetworkPeerType.Server)
    {
    GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Server");
    if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
    {
    Network.Disconnect(200);
    }
    }
    }
    }


    This code is attached at Cube object at both side server and client:

    using UnityEngine;
    using System.Collections;

    public class collide : MonoBehaviour {
    //public GameObject target;
    public Transform cubePrefab;
    public NetworkView nView;
    void Start() {
    nView = GetComponent<NetworkView>();
    }
    void OnGUI() {
    if (GUILayout.Button("SpawnBox")) {
    NetworkViewID viewID = Network.AllocateViewID();
    nView.RPC("SpawnBox", RPCMode.Server, viewID, transform.position);
    }
    }
    [RPC]
    void SpawnBox(NetworkViewID viewID, Vector3 location) {
    Transform clone;
    clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform;
    NetworkView nView;
    nView = clone.GetComponent<NetworkView>();
    nView.viewID = viewID;
    }
    }