I can drag and multiselect in host but not in client

Hello, I am doing the course right now and I realised that I can drag but not select as a client. The square appears, I can single select and holding shift select other units but I can not select anything when I drag as a client.

Please help I have been hours serching for the problem I can not find it :frowning:

Here is my player code

[SerializeField] private List<Villager> myUnits = new List<Villager>();

    public List<Villager> GetMyUnits()
    {
        return myUnits;
    }

    #region Server

    public override void OnStartServer()
    {
        Villager.ServerOnUnitSpawned += ServerHangleUnitSpawned;
        Villager.ServerOnUnitDespawned += ServerHangleUnitDespawned;
    }

    public override void OnStopServer()
    {
        Villager.ServerOnUnitSpawned -= ServerHangleUnitSpawned;
        Villager.ServerOnUnitDespawned -= ServerHangleUnitDespawned;
    }

    private void ServerHangleUnitSpawned(Villager unit)
    {
        if(unit.connectionToClient.connectionId != connectionToClient.connectionId) { return; }
        myUnits.Add(unit);
    }
    private void ServerHangleUnitDespawned(Villager unit)
    {
        if (unit.connectionToClient.connectionId != connectionToClient.connectionId) { return; }
        myUnits.Remove(unit);
    }

    #endregion

    #region Client

    public override void OnStartClient()
    {
        base.OnStartClient();
    }

    public override void OnStopClient()
    {
        if (!isOwned) { return; }
        Villager.AuthorityOnUnitSpawned -= AuthorityHangleUnitSpawned;
        Villager.AuthorityOnUnitDespawned -= AuthorityHangleUnitDespawned;

    }

    private void AuthorityHangleUnitSpawned(Villager unit)
    {
        if (!isOwned) { return; }
        myUnits.Add(unit);
    }
    private void AuthorityHangleUnitDespawned(Villager unit)
    {
        if (!isOwned) { return; }
        myUnits.Remove(unit);
    }

and this is my unit Selection Handler

 [SerializeField]
    private RectTransform unitSelectionArea = null;

    [SerializeField]
    private LayerMask layerMask = new LayerMask();

    private Vector2 startPosition;


    private NetworkPlayer player;
    private Camera mainCamera;

    public List<Villager> selectedUnits { get; } = new List<Villager>();

    private void Start()
    {
        mainCamera = Camera.main;
    }

    private void Update()
    {
        if (player == null)
        {
            player = NetworkClient.connection.identity.GetComponent<NetworkPlayer>();
        }
        
        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            StartSelectionArea();
        }
        else if (Mouse.current.leftButton.wasReleasedThisFrame)
        {
            ClearSelectionArea();
        }
        else if (Mouse.current.leftButton.isPressed)
        {
            UpdateSelectionArea();
        }
    }

    private void StartSelectionArea()
    {
        if (!Keyboard.current.leftShiftKey.isPressed)
        {

            foreach (Villager selectedUnit in selectedUnits)
            {
                selectedUnit.Deselected();
            }

           selectedUnits.Clear();
        }

        unitSelectionArea.gameObject.SetActive(true);

        startPosition = Mouse.current.position.ReadValue();

        UpdateSelectionArea();
    }

    private void UpdateSelectionArea()
    {

        Vector2 mousePosition = Mouse.current.position.ReadValue();

        float areaWidth = mousePosition.x - startPosition.x;
        float areaHeight = mousePosition.y - startPosition.y;

        unitSelectionArea.sizeDelta = new Vector2(Mathf.Abs(areaWidth), Mathf.Abs(areaHeight));
        unitSelectionArea.anchoredPosition = startPosition + 
            new Vector2(areaWidth / 2, areaHeight / 2);

    }

    private void ClearSelectionArea()
    {
        unitSelectionArea.gameObject.SetActive(false);

        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            if (!Physics.Raycast(ray, out RaycastHit hit, layerMask)) { return;}

            if(!hit.collider.TryGetComponent<Villager>(out Villager Aldeano)) { return; }

            if(!Aldeano.isOwned) { return; }

            selectedUnits.Add(Aldeano);

            foreach (Villager selectedUnit in selectedUnits)
            {
                selectedUnit.Selected();
            }

            return;


        } 
            Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
            Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

            foreach (Villager units in player.GetMyUnits())
            {
                if (selectedUnits.Contains(units)) { continue; }

                Vector3 screenPosition = mainCamera.WorldToScreenPoint(units.transform.position);

                if (screenPosition.x > min.x &&
                    screenPosition.x < max.x &&
                    screenPosition.y > min.y &&
                    screenPosition.y < max.y)
                {
                    selectedUnits.Add(units);
                    units.Selected();
                }
        
        }

   
    }

Hi there,
In don’t see your subscription to AuthorityHandleUnitSpawned and AuthorityHandleUnitDespawned.

If that is missing that would be the main issue here.

I did not realized that was missing. Thanks!!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms