How To Detect Mouse Click Or Touch On A GameObject Using C# Script In Unity 3D

Introduction

 
This article is about how to detect mouse click or touch on a GameObject using C# Script in Unity
 
Prerequisites
 
Unity Environment version 2018.4.25f1
 

Create the project

 
 
 I pressed ctrl + s to save this scene  
 

Create the Script

 
Add a new C# Script, Right-click on Assets. Select Create >> C# script.
 
 
Rename the script as SceneOneScript.
 
 

Create Empty GameObject in Unity

 
Click on the "GameObject" menu in the menu bar. Select "Create Empty" option.
 
 
 
Rename the Create Empty as Scripts
 
 
Select the Right click create new scene
 
 
I am going to rename the scene as SceneTwo
 
 
Double click to open it create another script scene - Script again Right-click on Assets. Select Create >> C# script.
 
 
 
Rename the Script as SceneTwoScript
 
 

Create Empty GameObject in Unity

 
click on the "GameObject" menu in the menu bar. Select "Create Empty" option. 
 
 
Rename the Create Empty as Scripts
 
 
Drag and drop your script to the scripts 
 
 
Make sure to add your scenes in the Build Settings window, choose “Build Settings” Add open scene.
 
 
 
Double click in Scene One and it will open Visual Studio 2019, which is my programming integrated development environment (IDE) for Unity. Write the code as shown below:
 
  
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using UnityEngine.SceneManagement;  
  5.   
  6. public class SceneOneScript: MonoBehaviour {  
  7.   // Start is called before the first frame update    
  8.   void Start() {}  
  9.   
  10.   // Update is called once per frame    
  11.   void Update() {  
  12.     if (Input.GetMouseButtonDown(0)) {  
  13.       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
  14.       RaycastHit hit;  
  15.       if (Physics.Raycast(ray, out hit)) {  
  16.         //Select stage    
  17.         if (hit.transform.name == "Cube") {  
  18.           SceneManager.LoadScene("SceneTwo");  
  19.         }  
  20.       }  
  21.     }  
  22.   }  
  23. }   
Save the Program
 
Create a cube in Unity.
 
Click on game object 3d and then I'm going to create a cube.
 
 
 
Click on SceneTwo open the scene
 
 
 

Create The Sphere in Scene View

 
Select GameObject, click the 3D object and pick the Sphere option. The Sphere object will be displayed in the scene view. The name is displayed in the hierarchy view.
 
 
InScene Two, double click it and it will open Visual Studio 2019, which is my programming integrated development environment (IDE) for Unity. Write the code as shown below:
 
 
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using UnityEngine.SceneManagement;  
  5.   
  6. public class SceneTwoScript: MonoBehaviour {  
  7.   // Start is called before the first frame update    
  8.   void Start() {}  
  9.   
  10.   // Update is called once per frame    
  11.   void Update() {  
  12.     if (Input.GetMouseButtonDown(0)) {  
  13.       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
  14.       RaycastHit hit;  
  15.       if (Physics.Raycast(ray, out hit)) {  
  16.   
  17.         //Select stage    
  18.         if (hit.transform.name == "Sphere") {  
  19.           SceneManager.LoadScene("SceneOne");  
  20.         }  
  21.       }  
  22.     }  
  23.   }  
  24. }  
Save the Program
 
Play the Application Click the sphere to SceneOne and Click the Cube to SceneTwo as You can see is navigating properly so this is the simple way you can tap or click on any gameobject
 
 

Summary

 
I hope you understood how to detect mouse click or touch on a GameObject Using C# Script in Unity.


Similar Articles