unity-game-engineunity3d-mirror

How to get other player's value in Unity when I use Mirror Network?


Hello? I'm studying the MIRROR Network Now.

enter image description here

However, I have a problem about getting other player's value. This image explain what I want to do.

enter image description here

I create 3 projects. One is server(local host) and other is Client A , the other is Client B.

Firstly, I wrote code like this :

public class PlayerManager : NetworkBehaviour
{
    [SyncVar(hook = nameof(onValueChanged))]
    int value = 0;

    private void Update()
    {
        if(isServer && Input.GetKeyDown("x"))
        {
            Message();
        }
    }

    public override void OnStartServer()
    {
        Debug.Log("Here is Game Room Scene, Player add Successfully");
    }

    [Command] 
    private void Hola()
    {
        value++;   

        Debug.Log("Received Hola from the client!");
        Debug.Log("Server Value : " + value);
        ReplyHola();
    }

    [TargetRpc]
    private void ReplyHola()
    {
        Debug.Log("Received Hola from Client!");
    }

    [ClientRpc]
    private void Message()
    {
        Debug.Log("Ping...");
    }

    [ClientRpc]
    private void UpdateValue(int value)
    {
        this.value = value;
    }

    private void onValueChanged(int oldValue, int newValue)
    {
        Debug.Log("New Value Detective :");
        Debug.Log("Old Value : " + oldValue);
        Debug.Log("New Value : " + newValue);
        Debug.Log("Sum Value : " + PlayerStat.Value);
    }
}

3 Projects have all same code. I referenced code from this video(https://www.youtube.com/watch?v=8tKFF0RP9Jw).

And, I wrote code about sum client A and B's code like this:

private void SumDatas()
{
    foreach(var playerObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
    {
        if(gameObject.name == "Player(Clone)")
        {
            PlayerStat.Value += GameObject.Find("Player(Clone)").transform.GetComponent<PlayerManager>().GetValue();
        }
    }
}

PlayerStat is a static class and Code is like this:

public static class PlayerStat
{
    public static int Value { get; set; }
}

Is there anybody help me?


Solution

  • I solved this problem. I will not delete this question for other people who will have same problem with me.

    I add this code at my server project, Client A and B project:

     [SyncVar(hook = nameof(onValueChanged))]
     int value = 0;
    
     int myValue = 0;
    
     private void Update()
     {
        myValue = PlayerStat.Value;   
     }