Object reference not set to an instance of an object

I am having a problem at the moment of spawning a building. Everything works fine until the moment the building shouls spawn, instead of spawning this mesage apears

NullReferenceException: Object reference not set to an instance of an object
BuildingButton.PlaceBuilding () (at Assets/_Game/_Scripsts/Buildings/BuildingButton.cs:83)

In that line es the player.CmdTryPlaceBuilding and for some reason it has a null reference. If I just comment that line there is no errors. Is I debug the get id and the hit point they apear on the console so the problem should be on the player but I can not find the error. Also I already check a thousand times in Unity and everything in the editor is as in the lecture.

This is my player script

[SerializeField] private Building[] buildings = new Building[0];

    private List<Villager> myUnits = new List<Villager>();
    private List<Building> myBuildings = new List<Building>();
    

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

    public List<Building> GetMyBuilings()
    {
        return myBuildings;
    }

    #region Server

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

        Building.ServerOnBuildingSpawned += ServerHangleBuildingSpawned;
        Building.ServerOnBuildingDespawned += ServerHangleBuildingDespawned;
    }

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

        Building.ServerOnBuildingSpawned -= ServerHangleBuildingSpawned;
        Building.ServerOnBuildingDespawned -= ServerHangleBuildingDespawned;
    }

    [Command]
    public void CmdTryPlaceBuilding(int buildingID, Vector3 buildingLocation)
    {
        Building buildingToPlace = null;

        foreach (Building building in buildings)
        {
            if (building.GetId() == buildingID)
            {
                buildingToPlace = building;
                break;
            }
        }

        if (buildingToPlace == null) { return; }

        GameObject buildingInstace =
            Instantiate(buildingToPlace.gameObject, buildingLocation, buildingToPlace.transform.rotation);

        NetworkServer.Spawn(buildingInstace, connectionToClient);
    }
    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);
    }

    private void ServerHangleBuildingSpawned(Building building)
    {
        if (building.connectionToClient.connectionId != connectionToClient.connectionId) { return; }
        myBuildings.Add(building);
    }
    private void ServerHangleBuildingDespawned(Building building)
    {
        if (building.connectionToClient.connectionId != connectionToClient.connectionId) { return; }
        myBuildings.Remove(building);
    }
    #endregion

    #region Client

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

        Villager.AuthorityOnUnitSpawned += AuthorityHangleUnitSpawned;
        Villager.AuthorityOnUnitDespawned += AuthorityHangleUnitDespawned;

        Building.AuthorityOnBuildingSpawned += AuthorityHangleBuildingSpawned;
        Building.AuthorityOnBuildingDespawned += AuthorityHangleBuildingDespawned;
    }

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

        Villager.AuthorityOnUnitSpawned -= AuthorityHangleUnitSpawned;
        Villager.AuthorityOnUnitDespawned -= AuthorityHangleUnitDespawned;

        Building.AuthorityOnBuildingSpawned -= AuthorityHangleBuildingSpawned;
        Building.AuthorityOnBuildingDespawned -= AuthorityHangleBuildingDespawned;

    }

    private void AuthorityHangleUnitSpawned(Villager unit)
    {
        myUnits.Add(unit);
    }
    private void AuthorityHangleUnitDespawned(Villager unit)
    {
        myUnits.Remove(unit);
    }

    private void AuthorityHangleBuildingSpawned(Building building)
    {
        myBuildings.Add(building);
    }
    private void AuthorityHangleBuildingDespawned(Building building)
    {
        myBuildings.Remove(building);
    }

    #endregion

type or paste code here

The error is in BuildingButton.cs, not your player script. if player.CmdTryPlaceBuilding() is the only thing on that line, then it’s very likely that player is null.

1 Like

Thanks! the problem was that I missed the

    if (player == null)
    {
        player = NetworkClient.connection.identity.GetComponent<NetworkPlayer>();
    }
1 Like

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

Privacy & Terms