1
votes

i am using UNet. I have two gamers in scene. One of them is server one of them is client. I can get client data with [Command] and [ClientRPC] but i can't send data from server to client how can i solve this problem ? Note: The server is a player also.

Here is my sending data from client to server code :

[Command]
    void CmdSendDizilimToServer(string dizilim){
        if (isLocalPlayer)
            RpcSetPlayerDizilim(dizilim);
        else
            this.Sira = dizilim;
    }
[ClientRpc]
    void RpcSetPlayerDizilim(string dizilim){
        this.Sira = dizilim;
    }

'Dizilim' means 'Sequence'. 'Sira' means 'Order'.

Actually in server and client have their own 'Order' string. When the game begining one gameobject spawned. And i want server get client 'Order' script(i can do this) the client get server 'Order' string(i can't do that). How can i solve that ?

It is my spawned player object script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;

public class MultiPlayerOyunKontrol : NetworkBehaviour  {
    CreateCharacter cc;
    public string Sira;
    GameObject EnemyObject;
    [SyncVar (hook = "DizilimGetir")]
    string OyuncuDizilimi;
    // Use this for initialization
    void Start () {
        if (!isLocalPlayer)
        {
            this.gameObject.name = "Oyuncu2";
            OyuncuDizilimi = PlayerPrefs.GetString("Player Dizilim");
        }
        else
            gameObject.name = "Oyuncu1";


    }

    // Update is called once per frame
    void Update () {

    }


    public override void OnStartLocalPlayer(){
        //CmdSendNameToServer (PlayerPrefs.GetString("Player Name"));
        CmdSendDizilimToServer (PlayerPrefs.GetString ("Player Dizilim"));

    }
    [Command]
    void CmdDusmanAktif(){
        RpcAktiflestir ();

    }
    [Command]
    void CmdSendNameToServer(string nameToSend)
    {
        RpcSetPlayerName(nameToSend);
    }
    [Command]
    void CmdSendDizilimToServer(string dizilim){
        if (isLocalPlayer)
            RpcSetPlayerDizilim(dizilim);
        else
            this.Sira = dizilim;
    }
    [ClientRpc]
    void RpcAktiflestir(){
        EnemyObject =  GameObject.FindGameObjectWithTag ("DusmanKarakterleri");
        EnemyObject.SetActive (true);
    }
    [ClientRpc]
    void RpcSetPlayerName(string name)
    {
        this.gameObject.name = name;
    }
    [ClientRpc]
    void RpcSetPlayerDizilim(string dizilim){
        this.Sira = dizilim;
    }
    void DizilimGetir(string dizilim) {
        if(!isLocalPlayer)
        this.Sira = dizilim;
    }


}
1

1 Answers

0
votes

I solve my question. When the client spawned the OnStartLocalPlayer() triggered but for only Server. The server can read client datas but the client can't trigger that function. And i write this code.

 private void OnGUI()
    {
        if (isLocalPlayer)
        {
            if (GUI.Button(new Rect(0, 500, 100, 100), "Gonder"))
            {
               CmdSendDizilimToServer(PlayerPrefs.GetString("Player Dizilim"));
            }
        }
    }

after that when i click button the client can read server datas.