Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by frmusta · Jun 23, 2010 at 05:14 AM · gameobjectraycastonmousedownname

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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);
     }
 }

}

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jun 23, 2010 at 11:24 AM 0
Share

note: no need for gameObject after collider, name works from all components (and gives the gameobject name)

avatar image Yanger_xy · Mar 28, 2011 at 08:12 AM 0
Share

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; } }

avatar image Mike 3 · Mar 28, 2011 at 09:38 AM 0
Share

put it into a new question, comments are far too difficult to help through

avatar image wenhua · Jan 10, 2012 at 06:26 AM 0
Share

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..^^

avatar image eric1254 · Jan 25, 2014 at 07:08 AM 0
Share

Thanks for your answer it is a great help for me

avatar image
0

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");
}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Mar 22, 2012 at 07:50 PM 1
Share

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.

avatar image hijinxbassist · Mar 27, 2012 at 02:26 AM 0
Share

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....

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges