A
A
Andrey Eskov2018-12-15 17:38:10
Unity
Andrey Eskov, 2018-12-15 17:38:10

How to synchronize objects when using UNET?

Hello everyone, tell me how to synchronize PlayerLobby.
There is a NetworkLobbyManager, there is a prefab LobbyPlayer, and so there is a task so that when the player clicks "Finish", the object changes. So the changes are made, but the changes are not visible from the second player.

Picture
5c1510eae87ef488958348.png
Here is the player script
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

namespace Assets.Scripts.Controller.MainMenu
{
    public class LobbyPlayerController : NetworkLobbyPlayer
    {
        private Text _name;
        private GameObject _status;
        private GameObject _readyBtn;
        private GameObject _notReadyBtn;
        private GameObject _expelBtn;
        [SerializeField] private Sprite[] _statusImages;

        public Text Name
        {
            get
            {
                return _name;
            }
        }
        public GameObject Status
        {
            get
            {
                return _status;
            }
        }
        public GameObject ReadyBtn
        {
            get
            {
                return _readyBtn;
            }
        }
        public GameObject NotReadyBtn
        {
            get
            {
                return _notReadyBtn;
            }
        }
        public GameObject ExpelBtn
        {
            get
            {
                return _expelBtn;
            }
        }
        public Sprite[] StatusImages
        {
            get
            {
                return _statusImages;
            }
        }

        public void Awake()
        {
            _name = transform.Find("PlayerName").gameObject.GetComponent<Text>();
            _status = transform.Find("PlayerStatus").gameObject;
            _readyBtn = transform.Find("PlayerReadyBtn").gameObject;
            _notReadyBtn = transform.Find("PlayerNotReadyBtn").gameObject;
            _expelBtn = transform.Find("PlayerExpelBtn").gameObject;
        }

        void Start()
        {
            Name.text = GetInstanceID().ToString();

            if (isLocalPlayer)
            {
                ReadyBtn.SetActive(true);
            }

            if (isServer)
            {
                ExpelBtn.SetActive(true);
            }
        }

        public void OnClickReady()
        {
            ReadyBtn.SetActive(false);
            NotReadyBtn.SetActive(true);
            Status.GetComponent<Image>().sprite = StatusImages[1];
        }

        public void OnClickNotReady()
        {
            NotReadyBtn.SetActive(false);
            ReadyBtn.SetActive(true);
            Status.GetComponent<Image>().sprite = StatusImages[0];
        }

        public override void OnClientEnterLobby()
        {
            base.OnClientEnterLobby();
            transform.SetParent(GameObject.FindGameObjectWithTag("Respawn").transform);
        }
    }
}


In theory, I just replace the sprite of the Image component by clicking on the button.
OnClickReady and OnClickNotReady hang on the button.
I will be very grateful.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question