Getting a GameObject

Hello there,

I am trying to get a script to give me the current GameObject with a mouse down.

Basically I have a cube with a box collider, nothing special, and I have a script attached to it that just prints hello when you click the cube.

What I need to know is how I can get the script which is attached to 2 cubes to tell me which cube I have clicked…
I have both cubes generated from prefabs and they are tagged separately.
I have a working function to compare the tags if i manually pass in the tag name.
I need a function to get the current GameObject that activated the script…

Any Ideas?

Thanks

This worked for me:

function OnMouseDown(){
	Debug.Log(gameObject.name);
}

I named the prefab ‘CubeClick’, but then renamed each game object in the Hierarchy ‘cube1’ ‘cube2’ ‘cube3’. When I click them, they each Debug.Log their hierarchy name.

If you want to get the tag, then use gameObject.tag instead.

Here is the gameObject API. Maybe there is something in there that might help, if what I posted isn’t what you were looking for.

That works for printing it out. Know of a way to attach that to a sting that I can use in my comparison function?

Figured it out.

I just made my string variable equal to gameObject.name

var myString = gameObject.name;

Thx for the advice :slight_smile: