LoginSignup
1
2

More than 3 years have passed since last update.

【Unity】マウスクリックで3Dオブジェクトを消すまで苦労した話

Last updated at Posted at 2019-07-18

はじめに

こんにちは。
今回はUnityに関することです。
Unityは初心者ですので、そこらへんのご理解お願いします。:bow:

この記事では、私が実際に、マウスクリックで3Dオブジェクトを消そうとした際に、とても苦労した話です。

最初に、消せるだろう手順を踏んだ流れを説明し、その次に消すための方法を記載しています。
この記事は一連の流れに沿って進むので、どうか飛ばさずに読んでいただけるとありがたいです。

環境

  • Windows 10 home
  • Unity 2018.2.15f1

消せるだろう手順を踏んだ流れ...

クリック1.PNG

このCubeを消すぞ!!:punch:

クリック2.PNG

Add Componentで「Event Trigger」を追加する。

クリック4.PNG

Add Componentでスクリプトを追加するぞ!
とりあえず名前は「cubeDestory」にした。

以下のコードを記述。

cubeDestroy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cubeDestroy : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void Destroy(){
        Destroy(this.gameObject);
    }
}
クリック5.PNG

Event TriggerのAdd New Event Typeで「Pointer Click」追加し、+マークからリストを追加。
そして、自身のCubeオブジェクトを選んで、先ほど作ったDestroy関数を選択する。

よし!これでクリックしたら消えると思ったけど、消えない...........:worried:
なぜだーーーーーーーーーー!!!!!:tired_face: :tired_face: :tired_face: :tired_face:

あと2つの作業で解決!

ここから、この問題を解決していきます。

クリック6.PNG

Main CameraにAdd Componentで「Physics Raycaster」を追加!

クリック7.PNG

最後に、Hierarchy → Create → UI → Event Systemで「Event System」を追加!!!

これでやっと、マウスクリックで3Dオブジェクトを消すことができた!!!!!!!:laughing: :laughing: :laughing: :laughing:

おわりに

解決できて、めっちゃ嬉しかった!:relaxed:
割と解決するまで、時間がかかったので、同じ悩みを抱えてる人がいたら、ぜひこの記事を見て、解決してほしいです!
ここまで読んでいただき、ありがとうございました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2