• 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 /
This question was closed Jan 29, 2017 at 11:53 AM by Aurel333 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Aurel333 · Jan 27, 2017 at 06:20 PM · c#raycastgraphic

Graphic raycast doesn't return true when clicking on the image

I want to set a boolean variable to true when clicking on the image (like selecting it) but it doesn't return true even if I click on it.

Here is my code:

     // Use this for initialization
     void Start ()
     {
         UnitPortrait1 = UnitPortrait1_Object.GetComponent<UnityEngine.UI.Image> ();
 
         UnitPortrait2 = UnitPortrait2_Object.GetComponent<UnityEngine.UI.Image> ();
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetKey (KeyCode.LeftControl)) {
             if (Input.GetKeyDown (KeyCode.Mouse0)) {
                 print ("test0");
                 if (UnitPortrait1.Raycast (Input.mousePosition, camera1)) {
                     print ("Image1 clicked");
 
                     Unit1Selected = true;
                     Unit2Selected = false;
                 }
                 if (UnitPortrait2.Raycast (Input.mousePosition, camera1)) {
                     print ("Image2 clicked");
         
                     Unit1Selected = false;
                     Unit2Selected = true;
                 }
             }
         }
     }

If you know what is wrong, or if there is a more efficient way to do what I want, please tell me.

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

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jan 28, 2017 at 01:38 PM

Graphic.Raycast can't be used to manually cast a ray against the graphic. It's just an internally used method to "filter out" invalid raycast positions. Make sure you read my comment as well ^^. I probably should include that in my answer over there.

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
avatar image
0

Answer by FortisVenaliter · Jan 27, 2017 at 08:19 PM

Well, the mouse is not a key, so I doubt GetKeyDown would ever trigger for it. Why not use GetMouseButtonDown instead?

Comment
Add comment · Show 1 · 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 Aurel333 · Jan 27, 2017 at 09:51 PM 0
Share

I tested and the get$$anonymous$$eyDown works. the problem is the raycast.

avatar image
2

Answer by PizzaPie · Jan 27, 2017 at 07:42 PM

Why bother with raycast on UI elements when you can use events. Add the event OnClick() on each image on the editor and assign the functions you want to run OnClick (the easy way) or make use of the IPointerClickHandler or IPointerSelectHandler (a bit more advanced).

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 Aurel333 · Jan 29, 2017 at 02:20 AM 0
Share

Ok, so I have figuered out that there was a much simpler way to do what I want.

I use buttons ins$$anonymous$$d of images and as @PizzaPie said, I used OnClick() to call the function I want on an other object in the scene. This object has one script with all the function I want to use, so it is much easier to use as all variables are in the script.

avatar image PizzaPie Aurel333 · Jan 29, 2017 at 03:12 AM 0
Share

How the hell my comment became an answer??? :D anyway to the point, you don't have to use a button as you can attach the a component: event-> OnClick to any UI element in case you don't want the fancy staff that comes with a button it is better to use an image as it requires much less memory. In great scales for a Pc it might have no difference but for a mobile it will.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I play the unity free walk animation on raycast(click) and continue to play until the character has arrived at the raycast destination? 0 Answers

Google Daydream Controller pick up objects 0 Answers

Errors from raycast in C# 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