I am creating a non-grid based procedural generation system, and I am using empty game objects as pointers to where the doors are. Each of these pointers has a tag on it that indicates it is a pointer, so I can grab all of them in the scene and select one randomly. When I instantiate a new room, I change the tag on the pointer so that it wont attempt to spawn a new room on an occupied door.
My issue is coming up when I try to spawn the same type of room more than once. After the object is spawned and the tag is changed, the prefab is being updated to have the new removed tags on that door, so every new instance of that room is broken. this issue persists out of play mode and I have to edit the prefabs in the editor to get them back to their correct state.
Here is the code (pointers are called jigsaws in the code):
private bool testRoomPlacement(GameObject selectedRoom)
{
bool canBePlaced = false;
GameObject testRoom = Instantiate(selectedRoom);
List<Jigsaw> testRoomJigsaws = new List<Jigsaw>();
testRoomJigsaws = testRoom.GetComponentsInChildren<Jigsaw>().ToList<Jigsaw>();
while (testRoomJigsaws.Count > 0)
{
Jigsaw randomJigsaw = testRoomJigsaws[1];
testRoom.GetComponent<BoxCollider>().enabled = false;
testRoom.GetComponent<BoxCollider>().enabled = true;
MatchExits(selectedJigsaw, randomJigsaw.gameObject, testRoom);
if (!testRoom.GetComponent<RoomCollisionValidator>().isColliding)
{
randomJigsaw.tag = "Untagged";
selectedJigsaw.tag = "Untagged";
Debug.Log("Room Fits, Placing");
canBePlaced = true;
break;
}
else
{
Debug.Log("Room does Not Fit on jigsaw, Retrying");
testRoomJigsaws.Remove(randomJigsaw);
continue;
}
}
if (testRoomJigsaws.Count == 0)
{
Debug.Log("Room does not fit in space, breaking");
Destroy(testRoom);
canBePlaced = false;
}
return canBePlaced;
}
Somewhere you are changing the tag of a prefab in the asset folder? You can do a test to see if an object you're setting a tag on is a prefab: if (someGameObject.scene.name == null). If this is true, the object is a prefab, which means it resides in the asset folder. See https://discussions.unity.com/t/how-to-know-if-a-gameobject-is-a-prefab/36753. Use this to check if randomJigsaw or selectedJigsaw is actually a prefab.
– ArachnidAnimal