The most recent player doesn't show in the inspector under its RtsPlayer script

The most recent player doesn’t show in the inspector under its RtsPlayer script. I think that this is because of the list not updating between players. Can anyone see an issue with my code?

Thanks in advance.

Unit script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using UnityEngine.Events;
using System;

public class Unit : NetworkBehaviour
{
    [SerializeField] private unitMovement movementScript;
    [SerializeField] private UnityEvent onSelected;
    [SerializeField] private UnityEvent onDeselected;


    public static event Action<Unit> ServerOnUnitSpawned;
    public static event Action<Unit> ServerOnUnitDespawned;

    public static event Action<Unit> AuthorityOnUnitSpawned;
    public static event Action<Unit> AuthorityOnUnitDespawned;

    public unitMovement GetUnitMovement()
    {
        return movementScript;   
    }


    #region Client
    public override void OnStartClient()
    {
        if (!isClientOnly) return;
        if (!hasAuthority) return;

        AuthorityOnUnitSpawned?.Invoke(this);
    }        
    
    public override void OnStopClient()
    {
        if (!isClientOnly) return;
        if (!hasAuthority) return;

        AuthorityOnUnitDespawned?.Invoke(this);
    }

    public void Select()
    {
        if (!hasAuthority) return;
        
        
        onSelected?.Invoke();
    }

    
    public void Deselect()
    {
        if (!hasAuthority) return;
        onDeselected?.Invoke();
    }

    #endregion

    #region Server

    public override void OnStartServer()
    {
        ServerOnUnitSpawned?.Invoke(this);
    }

    public override void OnStopServer()
    {
        ServerOnUnitDespawned?.Invoke(this);
    }

    #endregion

}

Rts player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class RTSPlayer : NetworkBehaviour
{
    [SerializeField]private List<Unit> myUnits = new List<Unit>();

    public List<Unit> getMyUnit()
    {
        return myUnits;
    }

    #region Server
    public override void OnStartServer()
    {
        Unit.ServerOnUnitSpawned += ServerHandleUnitSpawned;
        Unit.ServerOnUnitDespawned += ServerHandleUnitDespawned;
     }

    public override void OnStopServer()
    {
        Unit.ServerOnUnitSpawned -= ServerHandleUnitSpawned;
        Unit.ServerOnUnitDespawned -= ServerHandleUnitDespawned;
    }

   
    private void ServerHandleUnitSpawned(Unit unit)
    {
        if (unit.connectionToClient.connectionId != connectionToClient.connectionId) return;

        myUnits.Add(unit);
    }      
    
    private void ServerHandleUnitDespawned(Unit unit)
    {
        if (unit.connectionToClient.connectionId != connectionToClient.connectionId) return;

        myUnits.Remove(unit);
    }

#endregion
    #region Client
    public override void OnStartClient()
    {
        if (!isClientOnly) return;

        Unit.AuthorityOnUnitSpawned += AuthorityHandleUnitSpawned;
        Unit.AuthorityOnUnitDespawned += AuthorityHandleUnitDespawned;
    }

    public override void OnStopClient()
    {
        if (!isClientOnly) return;

        Unit.AuthorityOnUnitSpawned -= AuthorityHandleUnitSpawned;
        Unit.AuthorityOnUnitDespawned -= AuthorityHandleUnitDespawned;
    }



    private void AuthorityHandleUnitSpawned(Unit unit)
    {
        if (!hasAuthority) return;
        
        myUnits.Add(unit);
    }       
    private void AuthorityHandleUnitDespawned(Unit unit)
    {
        if (!hasAuthority) return;

        myUnits.Remove(unit);
    }
    #endregion
}



Found the solution, didn’t focus well during the lecture.

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

Privacy & Terms