Replace GameObject with another game object when button click event

when i am click first button then spawning some cube,
when i am click second button then change a replace with first gameobjects

bool hasChanged;
GameObject primitive;
GameObject yourObject;

public void Method()
{
   if(!hasChanged)
   {
          primitive = GameObject.CreatePrimitive(PrimitiveType.Cube);
          yourObject.SetActive(false);
    }
   else
   {
          Destroy(primitive);
          yourObject.SetActive(true);
   }

    hasChanged = !hasChanged;
}

Do this:

public GameObject firstObj, secondObj; // set these in the Inspector to prefabs
GameObject firstObjInstance, secondObjInstance;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        firstObjInstance = Instantiate(firstObj);
        if (secondObjInstance) { Destroy(secondObjInstance); }
    }
    if (Input.GetMouseButtonDown(1))
    {
        secondObjInstance = Instantiate(secondObj);
        if (firstObjInstance) { Destroy(firstObjInstance); }
    }
}

GameObject Object1, Object2

// attach this function to your button 
public void SwapFunciotn(){
GameObject Temp;
Temp = Object1;
Object1 = Object2;
Object2 = Temp;
}