I'm trying to spawn in a floor in an arbitrary location, as opposed to deploy it, so I can have a "starting point" for a properly decaying building somewhere like undeground/inside rock. Apart from the fact that it has floors instead of foundations and those floors are technically floating in the air, this building would follow every other rule of stability/placing structures. Including decaying the spawned in floor.
This is what I do currently. I spawn a floor in the middle of a default, empty map (size 1000).
var newBlock = GameManager.server.CreateEntity("assets/prefabs/building core/floor/floor.prefab", givenPosition, givenRotation) as BuildingBlock;
newBlock.Spawn();
newBlock.SetHealthToMax();
var groundWatch = newBlock.gameObject.GetComponent<GroundWatch>();
if (groundWatch != null)
{
UnityEngine.Object.DestroyImmediate(groundWatch);
}
var groundMissing = newBlock.gameObject.GetComponent<DestroyOnGroundMissing>();
if (groundMissing != null)
{
UnityEngine.Object.DestroyImmediate(groundMissing);
}
newBlock.grounded = true;
newBlock.buildingID = BuildingManager.server.NewBuildingID();
newBlock.AttachToBuilding(newBlock.buildingID);I manually deploy a TC on TOP of the spawned-in floor. The TC properly creates building priviledge etc, it takes into account the spawned-in floor undernath and any manually built floors attached to the original one with a Building Plan. I ent kill some floors, including the original spawned in-one, everything's fine.
When a floor is built on the side of the manually spawned in floor, I do the same stability fixes (groundWatch, groundMissing, grounded = true), so they should be acting the same as the spawned in-one, for all intents and purposes.
But the moment I ent kill all floors in the vicinity and spawn in a new one, this error starts happening:
NullReferenceExceptionServer Exception: Building ManagerLooking deeper in the output_log...
NullReferenceExceptionIt keeps spamming my console every minute/two, even with all "floating" floors gone.
at (wrapper managed-to-native) UnityEngine.Component.get_transform(UnityEngine.Component)
at BaseEntity.WorldSpaceBounds () [0x00000] in <689f722ee33f463fba3636a19cbc5561>:0
at BaseEntity.GetBuildingPrivilege () [0x00000] in <689f722ee33f463fba3636a19cbc5561>:0
at DecayEntity.GetBuildingPrivilege () [0x00011] in <689f722ee33f463fba3636a19cbc5561>:0
at DecayEntity.DecayTick () [0x0006c] in <689f722ee33f463fba3636a19cbc5561>:0
at ServerBuildingManager.Cycle () [0x00157] in <689f722ee33f463fba3636a19cbc5561>:0
at ServerMgr.Update () [0x000a7] in <689f722ee33f463fba3636a19cbc5561>:0
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception, Object)
ServerMgr:Update()
(Filename: <689f722ee33f463fba3636a19cbc5561> Line: 0)
When I restart the server, no more error. Until I decide to remove everything and spawn a new one in.
What am I doing wrong? I'm assuming I must be missing something crucial with BuildingManager, or when spawning in the first floor. Am I supposed to manually remove buildings when the last entity in my simulated building dies, or something?
Also, when a player slaps a foundation down in terrain, does it automatically add that foundation to a newly created building? If not, what triggers it? Deploying a tool cupboard in the vicinity? Attaching another building block to it? I thought it would be when you deploy a TC, but when I deployed it without setting the spawned-in floor's building ID and Attaching it to the building, the TC would display a message about the building decaying, but the resource cost was showing up as blank. Putting resources in didn't change anything.

So yeah, the problem was, I was setting the building ID in vain. The building ID is assigned when the entity is attached to a building with a new ID before spawning!