Destroying previus object when instantiatning a new one

I’m Building a City sim game and I have UI buttons thats is Instantiating a Gameobject.

When a player is pressing a another UI button to build a different building I want to destroy the previous buildng that is currently following the mouse.
I have been struggling on this for a while and can’t seem to work it out.

public void buildHouse()
    {
        Debug.Log(buildings[0]);
        ResourceManager.instance.buildCost(200);
        Instantiate(buildings[1], new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z), Quaternion.Euler(new Vector3(0, 0, 0)));
 }

This is the same for different buildings. I’m not sure if I have made myself clear but i want to add a way to replace a building if there is something allready following the mouse and not beeing yet placed.

Why don’t you just keep the reference of the GameObject you are istantiating (that is not yet placed) and destroy it on a button press or am I not understood you correctly?
Something like:
[SerializeField]
GameObject currentObjectFollowingTheMouse;

public void buildHouse()
{
     if (currentObjectFollowingTheMouse != null) 
     {
          Destroy(currentObjectFollowingTheMouse );
     }
     .
     .
     .
}

Thanks That worked I changed so the object is a ghost object that is placing the real building, so the code works now thank you again! :slight_smile: