- Home /
OnMouseDown to return object name
Im trying to click on pieces of geometry, and set a variable equal to the name of the object that i have clicked on. I have a collider on everything i want to interact with, and i am used to doing these things by applying a script directly to the object and using OnMouseDown but im having a hard time with this one. I dont really know where to start. It seems simple but im stuck with the fact that the script running this will be attached to the camera not the game object. I think i need to:
cast a ray from the mouse, so i think i can still use the OnMouseDown function to achieve this but im not sure how to access information about this clicks raycast so i can use something like: collider.gameObject.name
Found a few things that are close but not sure how to apply them. The raycast system is a bit over my head at this point, any tips you could toss my way would be greatly appreciated! Thanks for taking a look at my question.
Answer by Mike 3 · Jun 23, 2010 at 11:23 AM
OnMouseDown won't help with this much - it's a function which is called only when you click on that specific object
you'll want to do the raycasting in Update most likely, something like this:
function Update() { //check if the left mouse has been pressed down this frame if (Input.GetMouseButtonDown(0)) { //empty RaycastHit object which raycast puts the hit details into var hit : RaycastHit; //ray shooting out of the camera from where the mouse is var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
//print out the name if the raycast hits something
Debug.Log(hit.collider.name);
}
}
}
note: no need for gameObject after collider, name works from all components (and gives the gameobject name)
I have tried like this, but it seems to not work at all! private void On$$anonymous$$ouseDown() { print("$$anonymous$$ouse down"); Ray ray = _curCam.ScreenPointToRay(Input.mousePosition); Debug.DrawRay(ray.origin, ray.direction, Color.red); RaycastHit hitInfo = new RaycastHit(); if(Physics.Raycast(ray, out hitInfo, 10000.0f) == true) { _transformRet = hitInfo.transform; print(_transformRet.name.ToString()); } else { _transformRet = null; } }
put it into a new question, comments are far too difficult to help through
what if i dont want it the hit.collider.name to display on debug.log,but i want the name to display on the scene is it posssible..^^
Answer by Delerna · Mar 20, 2012 at 08:18 PM
This is an old thread but I have an answer using OnMouseDown. I found this thread searching for answers/ideas for my first Unity game ... a 2D Isometric game in the style of Farm Story, Restaurant Story etc etc.
I am very new to Unity (but not software development) so I am not sure if this is the "best way" but it is a way that works .... and for me if a solution works and performance is acceptable then I will use it until I am forced to find a better way.
I came up with this idea because I have a grid of 100 1 Meter X 1 Meter floor tiles that I need to know which floor tile I clicked on in order to place some other game object on top of it.
I made the floor tile a prefab with this script
var Name="MyName"; function OnMouseDown() { Debug.Log("The " + Name + " was clicked"); }
I then copy/pasted the game objects name from HIERARCHY into it's Scripts Name parameter in the INSPECTOR for each instance of the prefab.
(Tedious I know but really no different to naming text boxes, buttons etc in tools like visual studio and only took a few minutes)
Now all I need to do is find out how to get a reference to the actual game object from that name.....I've seen something on this somewhere
EDIT Actually, no parameter variable is needed No copy paste instance names just use the THIS keyword
function OnMouseDown() { Debug.Log("The " + this.GameObject.name + " was clicked"); }
Well considering you would want to have all your input managed in one script, and not in a script on each object in your game, this is a bad idea.
and ins$$anonymous$$d of saying this.blah blah you could just say gameObject.name which will refer to the gameObject who has this script attached. I agree with $$anonymous$$eltdown, you want it on one script and not all over the place. tough to manage. You could run a for loop from a $$anonymous$$anager script to get all the gameobjects and return all the names to a String array....
Answer by Delerna · Apr 02, 2012 at 07:09 AM
Thanks for the advise which I take on board for consideration. I did say I didn't know if this was the best way. Just that it was a way from onMouseDown which was stated as not do-able.
I understand the need to make code as easy to find, follow and debug as possible but I don't really understand your logic here.
Why wouldn't I extend that to say that you would want all animation code for every animated object in one place, or even all code in one place.
From those statement I might then conclude that you should never put scripts on gameObjects because you will finish up with code scattered all over the place.
I thought the whole idea behind OOP was so that objects and the code that controls them would be together and separate from all the other objects and their code for the purpose of making projects easier to manage/change and debug.
I don't have code all over the place. I have 1 floortile gameObject with 1 script providing all of the floor tiles functions, one of which will be to create an instance of another object on top of the floortile when I click on it. I have 1 floor gameObject that creates multiple instances of the floortile.
This gives me 1 script to change to modify the behaviour of every floor tile that I create. I can have lots of floor tile instances each with an onMouseDown event that only ever gets called when I click on that instance and not every frame regardless of whether it needs to be like from Update.
If I need to change what the floor tiles do then I just go to that single game object in the project window and edit that single script which contains all the code for that game object.
Not trying to be argumentative here but I don't see how that is a bad idea. Maybe you can illuminate me.
Your answer
Follow this Question
Related Questions
Get object name from raycast. 1 Answer
Detecting name through OnMouseDown 1 Answer
Ray Casting - Trigger function 1 Answer
Need help with building system 1 Answer
How to store player model and then swap the model to an object I Raycast hit? 0 Answers