Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by idev66 · Oct 08, 2015 at 01:08 PM · c#unity5

Using RayCast to see if button clicked

I get the button using this:

 Button button = GameObject.FindGameObjectWithTag("MainCanvas").GetComponentInChildren<Button>();

And then I do:

 void Update()
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if (Physics.Raycast(ray, hit))
     {
             if (button.collider.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit))
 { //Code to run when button clicked
     }
  }
  }

However I keep getting errors on this. Any idea where i've gone wrong? Thanks

Comment
Add comment · Show 2
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 Dave-Carlile · Oct 08, 2015 at 01:14 PM 0
Share

What errors?

avatar image idev66 Dave-Carlile · Oct 08, 2015 at 01:20 PM 0
Share

Argument 1: Cannot convert from UnityEngine.Ray to UnityEngine.Vector3 Argument 1: Cannot convert from UnityEngine.RaycastHit to UnityEngine.Vector3

Component.collider is obsolete. Property: collider has been depricated

Component does not contain a definition for Raycast and no extension method 'Raycast' accepting a first argument type of 'Component' could be found

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OctoMan · Oct 08, 2015 at 01:23 PM

Normally you do:

 if(Physics.Raycast(ray,out hit,rayLength))
 {
     if(hit.collider.tag == "button")
     {
     //do what ever
     }
 }

If you tag the button , you dont need to get the component from somewhere, basicly. Or you try:

 if(Physics.Raycast(ray,out hit,rayLength))
 {
     if(hit.collider.gameObject == button)
     {
     //do what ever
     }
 }

Maybe it helps. Cheers
Edit: Don't forget to add a collider to your button ;)

Comment
Add comment · Show 8 · 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 idev66 · Oct 08, 2015 at 01:29 PM 0
Share

Doesn't this just see if the mouse is hovering over the object though, and not if it's been clicked?

avatar image OctoMan idev66 · Oct 08, 2015 at 01:33 PM 0
Share

Where i wrote

 //do what ever 

you just make the test like :

 if(Input.Get$$anonymous$$ouseButtonDown(0))
 {
 Debug.log("Oh no! The mouse button was clicked on that button!")
 }


avatar image idev66 · Oct 08, 2015 at 01:40 PM 0
Share

Thank you. And sorry what would I set rayLength as?

avatar image OctoMan idev66 · Oct 08, 2015 at 01:42 PM 0
Share

rayLenght is a float of the length the ray can be, maybe

 private float rayLength = 10f;

If you don't know the length of the ray you need, you can say:

 private float rayLength = $$anonymous$$athf.infinity;

Cheers and good luck

avatar image idev66 · Oct 08, 2015 at 01:54 PM 0
Share

Seems no message is returned when click button. Thanks will keep trying

avatar image OctoMan idev66 · Oct 08, 2015 at 02:17 PM 0
Share

It's the left mousebutton. I wonder what you want to achieve anyways. The new canvas system automaticly support OnClick events on Buttons, even Touch Input.

Why you need a raycast for this?

avatar image OctoMan OctoMan · Oct 08, 2015 at 02:18 PM 0
Share

Oh and if it's not responding it can be thats there is another collider infront of it! Debug the hit what you actually hit!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn added objects into a circle 1 Answer

how to do on single click fire and on drag appears the joystick and get the input from joystick 0 Answers

Movement Unit (RTS) 0 Answers

Car from Unity's standard assets giving me of null reference errors 0 Answers

How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer


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