はじめに
ボタンを押したら、ゲームオブジェクトを表示したり非表示したりする方法を紹介します。
SetActiveを使用せずに、コード要らずで実現できるので、今までわざわざスクリプトを書いていた人は是非参考にしてください。
ボタンの作成
まずはUnityでボタンを作成します。
Hierarchy から UI > Button を選択します。

Canvasに新しいButtonオブジェクトが追加されます。

ボタンを押したらオブジェクトを表示する
Buttonオブジェクトを選択し、Inspectorウィンドウで各種設定を行うことができます。
On Clickにある「+」をクリックします。ここでボタンを押した時に実行する内容を設定できます。

選択したいゲームオブジェクトをHierarchyからドラッグアンドドロップします。

No Functionをクリックし、GameObject > SetActive (bool)を選択します。

チェックボックスにチェックを入れると、ボタンを押した時に表示されるようになります。

あらかじめ表示するオブジェクトは非表示にしておきましょう。
これで、Unity上にボタンが作成され、設定を変更することができます。

ボタンを押すと、お城が表示されるようになりました。
おわりに
ボタンを押した時の処理は、オブジェクトの表示・非表示以外にもあります。
それらを活用すると、効率よくゲーム制作できそうです。
ボタンの見栄えを良くしたいときはこのアセットがおすすめです。