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 oliver-jones · Nov 22, 2010 at 07:36 PM · colliderselectclickablegetmousebuttondown

Clickable Object Problem - Help

Hello,

I have a cube that I have made clickable, that right now, just prints to the console if it has been clicked or not. It all works, if I click on the object is says 'Clicked' and when I click again on the object (like a toggle), it says 'Clicked OFF'.

Heres the problem, the click will activate no matter what I click on, as long as it has a collider, I would like the click to be specific to what ever I click on.

Here is my script:

var Clicked = false;

function Update() { //Make Click On if ( Input.GetMouseButtonDown(0) && Clicked == false){ var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var select = GameObject.FindWithTag("Select").transform; if (Physics.Raycast (ray, hit, 100.0)){ select.tag = "Unselect"; hit.collider.transform.tag = "Select"; print("clicked"); Clicked = true; } } //Make Click Off else if ( Input.GetMouseButtonDown(0) && Clicked == true){ var hitOff : RaycastHit; var rayOff : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var selectOff = GameObject.FindWithTag("Unselect").transform; if (Physics.Raycast (rayOff, hitOff, 100.0)){ selectOff.tag = "Select"; hitOff.collider.transform.tag = "Unselect"; print("clicked OFF"); Clicked = false; } } }

About My Scene:

I have no player controller, just a simple camera looking downwards.

Would appreciate any help, thanks

Comment
Add comment · Show 3
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 oliver-jones · Nov 22, 2010 at 07:37 PM 0
Share

ALSO: The script is a property of the cube that I want to click on

avatar image skovacs1 · Nov 22, 2010 at 08:24 PM 0
Share

In Unity, scripts are not "properties" of GameObjects, they are Components.

avatar image oliver-jones · Nov 22, 2010 at 09:26 PM 0
Share

Okay, Thanks. Its not working though

1 Reply

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

Answer by skovacs1 · Nov 22, 2010 at 08:20 PM

Your script will cast a ray. If the ray hits, it will change the select variable and set the tag of the object hit by the raycast.

If you want it to only happen on the object the script is attached to, you should check that you actually hit the object the script is attached to before doing anything.

Since your code is convoluted and inefficient, here is a much simpler piece of code that will do as you intend without you calling raycast or find or any of the other nonsense:

MouseDownSelection.js

var clicked : boolean = false;
function OnMouseDown() {
    clicked = !clicked;
    if(tag == "Selected") tag = "Unselected";
    else tag = "Selected";
    Debug.Log("clicked" + (clicked? "" : " off"));
}

EDIT:

According to your comments, because you are trying to check a collider inside of another collider, you are trying to do something a bit more troublesome. The solution is to only raycast against a subset of colliders including the one you care to hit and excluding the one that is in your way. This can be done with layer masking or even more simply by casting against just the collider of the current object.

SpecificColliderMouseDownSelection.js

//Needed due to Unity js issue //Alternatively specify the type with (collider as Collider).Raycast... #pragma strict

var clicked : boolean = false; var hit : RaycastHit; function Update() { if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) { clicked = !clicked; if(tag == "Selected") tag = "Unselected"; else tag = "Selected"; Debug.Log("clicked" + (clicked? "" : " off")); } }

If you used a Physics.Raycast with an appropriate layer mask, you would check and operate on the thing the raycast hit. If you have multiple selectable objects, that is a better way to go so that you can instead attach the script to the camera or something and only raycast once.

SingleSelection.js (only one per scene)

var selected : Transform; var mask : LayerMask = -1;

function Update() { if(Input.GetMouseButtonDown(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity, mask.value)) { if(hit.transform.tag == "Selected") { hit.transform.tag = "Unselected"; selected = null; } else if(!selected) { selected = hit.transform; selected.tag = "Selected"; } Debug.Log("clicked" + (selected? "" : " off")); } }

MultipleSelection.js (only one per scene)

var selection : Array = new Array(); var mask : LayerMask = -1;

function Update() { if(Input.GetMouseButtonDown(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity, mask.value)) { if(hit.transform.tag == "Selected") { hit.transform.tag = "Unselected"; var index : int = 0; for(;index < selection.Length; index++) { if(selection[index] == hit.transform) break; } if(index < selection.Length) selection.RemoveAt(index); Debug.Log("clicked off"); } else { hit.transform.tag = "Selected"; selection.Add(hit.transform); Debug.Log("clicked"); } } }

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 oliver-jones · Nov 22, 2010 at 08:47 PM 0
Share

This isn't working at all. It looks like it would, but it's not, Im not getting anything in my console, any suggestions why? I'm using an Orthographic camera if that would make any difference. I've also changed your tags from Selected, to Select (and Deselected - Deselect) as those are the tags I have

avatar image oliver-jones · Nov 22, 2010 at 09:38 PM 0
Share

Okay, it does work -- but heres my situation - I have a sphere collider going around a child within my cube (with trigger on - deter$$anonymous$$es range). The script has been applied to my cube, now the sphere collider is bigger than the cube collider therefore it does not register when I hit the cube collider, and I don't want to make the cube collider bigger as then the user will be able to select out of the cube ... Help

avatar image oliver-jones · Nov 22, 2010 at 09:39 PM 0
Share

<< Hope that made sense: I have a Cube (with original collider), a child of that cube, I have an empty (with a big sphere collider - trigger). Cube/Empty

avatar image skovacs1 · Nov 22, 2010 at 09:41 PM 0
Share

You should really have included that information in your question as that changes what you are trying to do a fair bit. I will post the code for that specific case.

avatar image oliver-jones · Nov 22, 2010 at 09:51 PM 0
Share

Sorry, Thanks Skovacs

Show more comments

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

No one has followed this question yet.

Related Questions

Hard to select certain objects (collider problem??) 2 Answers

Clickable area problem? 0 Answers

Click On Objects Within Scene 2 Answers

Why 2D colliders doesn't work? 1 Answer

Adding multiple simple mesh colliders to be able to click on an objects parent 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