Clicked object is stored in a variable?

I’m currently working on a piece of code that will allow me to move different objects depending on what I click on however I’m having some trouble finalizing it.

The idea is simple, I have a variable called target…

var target: Transform;

…and I use this variable throughout the code. Currently however I have to preset this to an object from my scene (drag and drop it into the slot). What I need to be able to do is basically say

target = object clicked on

I’ve looked into mouse ray casting but got the error “Cannot convert UnityEngine.GameObject to UnityEngine.Transform”.

if (Physics.Raycast (ray, hit, 100))
    	{
    		if(hit.collider.gameObject.tag == "Cube")
    		{
    			target = hit.transform.gameObject;
    		}
    	}

Any help would be greatly appreciated.

Your ‘target’ is a Transform, so line 5 should be:

target = hit.transform;