Hello, I am working on a 2d isometric grid building system, and I have encountered a problem with destroying already placed objects. So I have a prefab with a panel and buttons, one button for placement and the other for destroying the prefab clones. It works but only for the prefab clone that was placed last. If I try to destroy others I get this error “MissingReferenceException: The object of type ‘Building’ has been destroyed but you are still trying to access it.”.
Here is the code I use to instantiate objects
public void InstantiateObject(GameObject gameObject)
{
if(!objectUnplaced)
{
if (canBePlaced)
{
current=Instantiate(gameObject,Vector3.zero,Quaternion.identity).GetComponent<Building();
GridBuildingSystem.instance.FollowBuilding(current);
towerUnplaced = true;
}
}
}
This method is called from buttons script:
public void DestroyObject()
{
Destroy(gameObject);
}
I understand what the error means but I don’t really know how to solve it, does anybody have any suggestions on how to fix it?