using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;

public class NetworkActiveState : NetworkBehaviour
{
    private NetworkVariable<bool> activeState = new NetworkVariable<bool>(default, default, NetworkVariableWritePermission.Owner);

    public override void OnNetworkSpawn()
    {
        if (IsOwner) {
            activeState.Value = gameObject.activeSelf;
        }
        activeState.OnValueChanged += OnActiveStateChangedDelegate;

        base.OnNetworkSpawn();
    }

    void OnActiveStateChangedDelegate(bool previousValue, bool newValue)
    {
        if (IsOwner == false) {
            gameObject.SetActive(newValue);
        }
    }

    private void OnEnable()
    {
        if (IsOwner) {
            activeState.Value = true;
        }
    }

    private void OnDisable()
    {
        if (IsOwner) {
            activeState.Value = false;
        }
    }
}
