How can I ignore background under UI for clicking?

So I have gameobjects I can click on and drag anywhere on the screen.At the same time there are buttons to rotate,move and adjust their mass etc.So I write a script to background that when I click on background,I deselect my object and buttons for them disappear.But when I click my button(say rotate button) button works,but my click also detects background and my buttons disappear.How can I solve this?

Thanks in advance and sorry for my english.

When selecting objects(not ui), check if the pointer is above a ui element(asuming that you have an event system in the scene):

bool t = UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ();

if t is true then the pointer(mouse) is on top of a ui element and you should tell it to not do anything.
This can also work for touch but is a bit more involved.