Search code examples
c#visual-studiounity-game-engineunity3d-unet

Client doesnt get spawned gameObjects from server, and the other way around


So, I'm struggling with the whole Unet system. right now, all I want to do is to spawn object from one end to the other. but even that doesn't work. It spawns the object on each the server and the client. but doesn't sync. I've searched so many places and watched so many videos - none helped. here are the code and the inspector details

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkController : NetworkBehaviour
{
    public GameObject[] Spawns;
    GameObject Canvas;
    GameObject spawn;
    [SyncVar]
    public NetworkInstanceId ParentId;
    // Use this for initialization
    void Start()
    {
    }

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

    }

    [Command]
    public void CmdspawnPrefab()
    {
        Transform trns = GameObject.Find("Canvas").transform;
        trns.position = trns.position + new Vector3(100, 200, 0);
        GameObject go = Instantiate(Spawns[0], trns);
        NetworkServer.Spawn(go);
    }
}

enter image description here

What am I missing?


Solution

  • As it seems, my whole workflow was not correct. the solution is firstly to attach a listener to the button, instead of using the onclick function on the inspector -

            Button b = BoardController.Buttons[0].GetComponent<Button>();
            b.onClick.AddListener(delegate () { Cmd_ButtonPressed(1); });
    

    the second is to create gameobjects, containing a script "GameManager" who controls your board, and one "RpcManager" which contains the Rpc functions. then in the playerobject script, use the commands.

    Here are the classes i have used, to update an image instead of spawning an object, the idea is basically the same - to undersand the fundamentals of Unet and passing commands.

    public class PlayerObject : NetworkBehaviour {
    
    public BoardManager BoardController;
    public ClientRPCmanager ClientRPCManager;
    // Use this for initialization
    void Start () {
        ClientRPCManager = GameObject.FindGameObjectWithTag("ClientRPCmanager").GetComponent<ClientRPCmanager>();
        BoardController = ClientRPCManager.BoardController;
        Button b = BoardController.Buttons[0].GetComponent<Button>();
            b.onClick.AddListener(delegate () { Cmd_ButtonPressed(1); });
    }
    // Update is called once per frame
    void Update () {
    
    }
    
    public void SetButton(int i)
    {
        BoardController.SetButton(i);
    }
    [Command]
    public void Cmd_ButtonPressed(int i)
    {
        ClientRPCManager.Rpc_ButtonPressed(i);
    }
    

    boardmanager

    public class BoardManager : NetworkBehaviour
    {
    
    // Use this for initialization
    void Start()
    {
    
    }
    // Update is called once per frame
    void Update()
    {
    
    }
    
    public GameObject[] Buttons;
    public Sprite[] Sprites;
    public void SetButton(int index)
    {
        GameObject img = GameObject.Find("X");
        img.GetComponent<Image>().sprite = Sprites[0];
    }
    

    RpcManager

    public class ClientRPCmanager : NetworkBehaviour {
    
    public BoardManager BoardController;
    public NetworkManager Manager;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    
    [ClientRpc]
    public void Rpc_ButtonPressed(int index)
    {
        BoardController.SetButton(index);
    }
    

    hopefully, this will somewhat help people with understanding Unet. I learned this by carefully looking at this project https://www.youtube.com/watch?v=8Kd2RAfgzW0