Unity Two Method of Interacting Objects with Mouse

There are two simple methods of writing scripts for the mouse to interact with the game object. A simple method is to write a script for each game object where on mouse down or up, execute some code. However, you can also have a raycast to spawn at where the mouse is and if it hits an game object that you want, execute some code.

OnMouseDown()

This is one of the easiest script to write for a game object. All you need to do write the interaction code in the function OnMouseDown() and attach it to the game object.

Raycast

Unity ray cast can also do the trick, first make three variable, the Ray, the objects it hits, and lastly to store the Target of your choice. Inside the Update function, if we left click, we get a list of all the target hits, for then we check for tags. For each game object that has the tag target, we know we got the object and then we can do anything we want with it.