Can't place buildings, the list myBuilding is empty

has i said in the title, i find out by placeing some debug that myBuilding list is empty. thats the RTSplayer script

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

public class RTSPlayerScript : NetworkBehaviour
{
[SerializeField] private LayerMask buildingBlockLayer = new LayerMask();
[SerializeField] private Buildings buildings = new Buildings[0];
[SerializeField] private float buildingRangeLimit = 5f;

[SyncVar(hook = nameof(ClientHandleResourcesUpdate))]
[SerializeField] private int resources = 500;

public event Action<int> ClientOnResourcesUpdated;

[SerializeField] private List<Unit> myUnits = new List<Unit>();
private List<Buildings> myBuildings = new List<Buildings>();

public List<Unit> GetUnits()
{
    return myUnits;
}
public List<Buildings> GetBuildings()
{
    return myBuildings;
}
public int GetResources()
{
    return resources;
}

public bool CanPlaceBuilding(BoxCollider boxCollider, Vector3 spawnPoint)
{
    Debug.Log(myBuildings.Count);

    if (Physics.CheckBox(spawnPoint + boxCollider.center,
                boxCollider.size / 2,
                Quaternion.identity,
                buildingBlockLayer)) { return false; }

    foreach (Buildings building in myBuildings)
    {
        Debug.Log((spawnPoint - building.transform.position).sqrMagnitude);

        if ((spawnPoint - building.transform.position).sqrMagnitude <= buildingRangeLimit * buildingRangeLimit)
        {
            return true;
        }
    }
    return false;
}

[Server]
public void SetResources(int newResources)
{
    resources = newResources;
}

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

[Command]

public void CmdTryPlaceBuilding(int buildingId, Vector3 spawnPostion)
{
    Buildings buildingToPlace = null;

    foreach (Buildings building in buildings)
    {
        if(building.GetId() == buildingId)
        {
            buildingToPlace = building;
            break;
        }
    }

    if(buildingToPlace == null) { return; }

    if(resources < buildingToPlace.GetPrice()) { return; }

    BoxCollider buildingCollider = buildingToPlace.GetComponent<BoxCollider>();

    if(!CanPlaceBuilding(buildingCollider, spawnPostion)) { return; }

    GameObject buildingInstance =
        Instantiate(buildingToPlace.gameObject, spawnPostion, buildingToPlace.transform.rotation);

    NetworkServer.Spawn(buildingInstance, connectionToClient);

    SetResources(resources - buildingToPlace.GetPrice());
}

public override void OnStopServer()
{
    Unit.ServerOnUnitSpawned -= ServerHandleUnitSpawned;
    Unit.ServerOnUnitDespawned -= ServerHandleUnitDespawned;
    Buildings.ServerOnBuildingSpawned -= ServerHandleBuildingSpawned;
    Buildings.ServerOnBuildingDespawned -= ServerHandleBuildingDespawned;
}

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);
}
private void ServerHandleBuildingSpawned(Buildings building)
{
    if (building.connectionToClient.connectionId != connectionToClient.connectionId) { return; }

    myBuildings.Add(building);
}

private void ServerHandleBuildingDespawned(Buildings building)
{
    if (building.connectionToClient.connectionId != connectionToClient.connectionId) { return; }

    myBuildings.Remove(building);
}

#endregion

#region Client
public override void OnStartAuthority()
{
    if (NetworkServer.active) { return; }

    Unit.AuthorityOnUnitSpawned += AuthorityHandleUnitSpawned;
    Unit.AuthorityOnUnitDespawned += AuthorityHandleUnitDespawned;
    Buildings.AuthorityOnBuildingSpawned += AuthorityHandleBuildingSpawned;
    Buildings.AuthorityOnBuildingDespawned += AuthorityHandleBuildingDespawned;
}

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

    Unit.AuthorityOnUnitSpawned -= AuthorityHandleUnitSpawned;
    Unit.AuthorityOnUnitDespawned -= AuthorityHandleUnitDespawned;
    Buildings.AuthorityOnBuildingSpawned -= AuthorityHandleBuildingSpawned;
    Buildings.AuthorityOnBuildingDespawned -= AuthorityHandleBuildingDespawned;
}
private void AuthorityHandleUnitDespawned(Unit unit)
{
    myUnits.Add(unit);
}

private void AuthorityHandleUnitSpawned(Unit unit)
{
    myUnits.Remove(unit);
}
private void AuthorityHandleBuildingDespawned(Buildings building)
{
    myBuildings.Add(building);
}

private void AuthorityHandleBuildingSpawned(Buildings building)
{
    myBuildings.Remove(building);
}

private void ClientHandleResourcesUpdate(int oldResource, int newResource)
{
    ClientOnResourcesUpdated?.Invoke(newResource);
}


#endregion

}

i hope you can help me out

i’ve fixed the problem by adding the building script to the unit base.
but then i’ve found that the building had null reference for the box collider, so i’ve removed all box colliders in the childrend and addedo on the root of the prefabs.

1 Like

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

Privacy & Terms