Why can't the client move a networked object ?

Hello there my future saviors ! I am working on a multiplayer 2D chess game with UNET, but unfortunately got stuck. So, in my game one of the player is the Server as well as the Client at the same time. My idea is to instantiate all the objects, such as the Tiles and Pieces on the Server, and then use NetworkServer.Spawn to propagate the objects on the Clients, so that both of them can see what’s going on. However, when it comes to moving the pieces from the Client which is not the Server, the designated object does not move, despite that it recognizes the piece and all, whereas on the other Client-Server moving the same piece works perfectly.

Here is the how I call the functions which create my objects on the Server :

  void Start()
  {
          if (isServer && isLocalPlayer)
           {
             CreateBoard ();
             SetUpPieces ();
            Debug.Log (" SERVER ");
           }
  }

This is the Command function which moves the piece :

     [Command]
         public void CmdmoveBish(int x , int y)
         {
             Debug.Log ("Command the server to move bishop : ");
             bishop.transform.position = new Vector2 (x,y);
         }

I am sure that the command function is executed from both Clients, because I get the message in the console. Btw, I have noticed that when the non-Server-Client tries to move the piece it changes the coordinates of the piece prefab, instead of the object which was instantiated in the game. It sound like there’s a simple solution but I’ve become very confused. Additional information; this is how I instantiate my piece :

 public void SetUpPieces()
     {      
                         bishop = Instantiate (bishop);      // bishop is a GameObject
             bishop.transform.position = new Vector2 (2,0);
             NetworkServer.Spawn (bishop);
             Debug.Log ("bishop is spawned");
     }

I appreciate your help and thank you in advance :slight_smile:

Why are you trying to reuse the bishop prefab object as the instantiated object?

bishop = Instantiate (bishop);

Understand that on the server the bishop object now references a game piece in play, but on the client the bishop object references the prefab because you only call this instantiate line on the server.

As for your question, you should post the entire script. What you’ve posted doesn’t answer much.

Here is the code for the Board class which is attached to NetSpawn Gameobject (Player prefab):

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

public class Board : NetworkBehaviour
{
    public GameObject tile,tile1; // There are two diferent tiles for the different colors
    public GameObject pawn, bishop, knight, rook, queen, king;
    public GameObject SubBishop;
    public GameObject[,] squares = new GameObject[8, 8];

    void Start()
    {
        if (isServer && isLocalPlayer)
        {
            CreateBoard ();
            SetUpPieces ();
            Debug.Log (" SERVER ");
        }
    }

    public void CreateBoard()
    {
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                // changing colors of the tiles
                if ((i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0))
                {
                    squares[i, j] = Instantiate(tile, new Vector3(i, j, 0), Quaternion.identity);    
                    NetworkServer.Spawn(squares[i,j]);
                }
                else
                {
                    squares[i, j] = Instantiate(tile1, new Vector3(i, j, 0), Quaternion.identity);   
                    NetworkServer.Spawn(squares[i,j]);
                }
                squares[i, j].transform.SetParent(gameObject.transform);   
                BoxCollider2D aha = squares [i, j].AddComponent<BoxCollider2D> (); 

            }
        }
    }
    public void SetUpPieces()
    {
        SubBishop = Instantiate (bishop);
        SubBishop.transform.position = new Vector2 (2,0);
        NetworkServer.Spawn (SubBishop);
        Debug.Log ("bishop is spawned");
    }

    [Command]
    public void CmdmoveBish(int x , int y)
    {
        Debug.Log ("Commanded the server to move bishop : ");
        SubBishop.transform.position = new Vector2 (x,y);
    }
       
    //How the bishop moves: two clicks ; 1st click selects the bishop; 2nd click selects bishop's destination
    // How MouseClick works: it check the boolean value; if it's true then it means you need to select the bishop first
    // if bishop is selected, the boolean becomes false and when the destination, meaning tile is clicked, the boolean
    // is true again and the bishop is moved.

    public bool click1 = true; 
    public void MouseClick ()
    {
        if (click1) {
            if (Input.GetMouseButtonDown (0)) {
                Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                RaycastHit2D hit = Physics2D.Raycast (pos, Vector2.zero);

                if (hit.collider != null && hit.transform.gameObject.name == "Bishop(Clone)") {

                    Debug.Log ("First Click on the Bishop");
                    click1 = false;
                }
            }
        } else {            // it's false for the second CLICK ; The second click is the bishop's destination
            if (Input.GetMouseButtonDown (0)) {
                Vector3 pos1 = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                RaycastHit2D hit1 = Physics2D.Raycast (pos1, Vector2.zero);
                if (hit1.collider != null && hit1.transform.position == new Vector3 (5, 3, 0)) {  
                    CmdmoveBish (5, 3);
                    click1 = true;
                } else if (hit1.collider != null && hit1.transform.position == new Vector3 (3, 5, 0)) {
                    CmdmoveBish (3, 5);
                    click1 = true;
                } else if (hit1.collider != null && hit1.transform.position == new Vector3 (4, 4, 0)) {
                    CmdmoveBish (4, 4);
                    click1 = true;
                } else if (hit1.collider != null && hit1.transform.position == new Vector3 (6, 4, 0)) {
                    CmdmoveBish (6, 4);
                    click1 = true;
                }
            }
        }
    }

    void Update ()
    {
        if (isLocalPlayer ) {
            MouseClick ();
        }
    }
}

Also I’ve attached a screenshot of the player prefab spawned by the Network Manager.

Notes:

  1. The bishop can’t move to all the tiles. Square f4 (5,3) , e5 (4,4) , d6 (3,5) , g5 (6,4) are only accessible.
  2. I made a different object for instantiating the bishop, instead of reusing bishop.

P.S. I’m trying to use one object for both clients, instead of spawning a bishop for each client. Maybe that’s why the bishop cannot be referenced from both clients.
P.P.S. Thank you for your response!

Maybe try using the AssignClientAuthority method on the piece the client wants to move, and when that said client is done moving the piece, the authority is again removed from the client and given back to the scene/server/host.
https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity.AssignClientAuthority.html
https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity.RemoveClientAuthority.html
I hope this helps you into the right direction!
-Pablo Discobar