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
0
Question by MikezNesh · Jul 20, 2010 at 05:48 PM · gameobjecttagclickcheck

Is there something like clicked.gameObject?

Is there something like clicked.gameObject? (like col.gameObject)

Because I want to check the tag of the gameObject I clicked and do something with it.

EDIT: added script:

I'm trying to make it so if clicked tag is chopstik then selected = true. I know I'm not doing right though. How would I do it Properly?

function Update() { if (Input.GetButtonDown("Fire1")) { var hit : RaycastHit; if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) { Debug.Log(hit.collider.tag); if(hit.collider.tag == "Chopstik") { selected = true; } }

if (Input.GetMouseButtonUp (0)) {

 selected = false;
 print("deselected");

}

}

}

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
3
Best Answer

Answer by Mike 3 · Jul 20, 2010 at 08:09 PM

This will print the tag of the last clicked object:

function Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        var hit : RaycastHit;
        if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
        {
            Debug.Log(hit.collider.tag);
        }
    }
}
Comment
Add comment · Show 13 · 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 MikezNesh · Jul 20, 2010 at 08:20 PM 0
Share

How would I set it to find the magnitued between on click and on release?

avatar image Mike 3 · Jul 20, 2010 at 08:22 PM 0
Share

assign to a start var on mouse down (and set a bool to say you hit something or not), then use that in the mouse up if you hit something else to get the distance

avatar image MikezNesh · Jul 20, 2010 at 08:30 PM 0
Share

How would I get distance? everything else I know. Also how come with the new code my on mouse release thing stops working?

avatar image Mike 3 · Jul 20, 2010 at 08:43 PM 0
Share

Vector3.Distance(start, end). Not sure about why your new code doesn't work. Perhaps try GetButtonUp("Fire") if you don't want it mapped specifically to left mouse

avatar image MikezNesh · Jul 20, 2010 at 08:51 PM 0
Share

The code is like this? if (Input.GetButtonUp("Fire")) because it doesn't recognize my inout key.....

Show more comments
avatar image
2
Best Answer

Answer by equalsequals · Jul 20, 2010 at 06:07 PM

Since your click events are handled via a collider, you can check the collider component's game object by Collider.gameObject property. That returns a reference to the Game Object in which your Collider component is attached to. From there, the Game Object has a public property GameObject.tag which will return the tag of that object.

something like this:

function OnMouseDown():void { var clickedTag:String = collider.gameObject.tag; }

EDIT:

What you're doing wrong above is tracking in the Update. You don't need to. All of the info you'll need can be well encapsulated in the OnMouseDown event.

function OnMouseDown():void { var clickedTag:String = gameObject.tag;

 if(clickedTag == "Chopstik") selected = true;

}

Hope that helps.

==

Comment
Add comment · Show 9 · 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 MikezNesh · Jul 20, 2010 at 06:08 PM 0
Share

How about for javascript?

avatar image Mike 3 · Jul 20, 2010 at 06:44 PM 0
Share

That is javascript. You also don't need collider.gameObject, you can just use tag. Or just gameObject.tag.

avatar image MikezNesh · Jul 20, 2010 at 07:07 PM 0
Share

what if I wanted to do an if statement? edited question with my scripting problem.

avatar image equalsequals · Jul 20, 2010 at 07:42 PM 0
Share

I edited my answer

avatar image MikezNesh · Jul 20, 2010 at 08:04 PM 0
Share

applied code along with a print to tell me if it works. never print "selected". It doesn't seem able to tell when you click it. No compilation errors though. Please Help!

Show more comments
avatar image
0

Answer by loginpawan · Aug 27, 2012 at 08:20 AM

even thought i put the collider on imported model yet it not react with OnMouseOver() function my code is <

function Update () {

 if (Input.GetButton ("Fire1") )
 {

// isSelected = true; var clickedTag:String = gameObject.tag; if(clickedTag == "Melon") { selected = true;

Destroy (gameObject);

Debug.Log("Is selected "); }

} }

> Somebody please help 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

2 People are following this question.

avatar image avatar image

Related Questions

View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers

check if Go.tag is this instance of the object 1 Answer

Check if there is a child with a tag? (multiple children but each diff tag) 1 Answer

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

Shooting Specific GameObject via Tag 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