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 /
avatar image
0
Question by YuBen · Feb 05, 2019 at 07:12 PM · raycastraycasthitraycasthit2d

Raycast Not Working

Hey all,

I'm trying to make an object (with a box collider) interact with a click to exit out of a while loop using a Raycast. However, although I think I have all the components necessary to initiate it, nothing happens when I click on the object, and the while loop keeps looping. The snippet of code follows:

     public IEnumerator Qone()
     {
 
         t.text = "A: 1";
         while (i == 0)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Input.GetMouseButtonDown(0))
             {
                 if (Physics.Raycast(ray, out hit))
                 {
                     if (hit.transform.name == "A")
                     {
                         Debug.Log("hit");
                         i = 1;
                     }
                 }
             }
             Debug.Log("F");
             yield return null ;
         }
 
         i = 0;
         StopAllCoroutines();
     }
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 Ymrasu · Feb 05, 2019 at 07:27 PM 0
Share

Your code works for me. Can you post a screenshot of your Hierarchy and Inspector of the object in question?

avatar image YuBen Ymrasu · Feb 05, 2019 at 10:19 PM 0
Share

@Ymrasu Yes, here they are. The object in question is "A." (Don't $$anonymous$$d how unorganized they are)

hierarchy.png (10.8 kB)
inspector.png (68.2 kB)

3 Replies

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

Answer by YuBen · Feb 08, 2019 at 01:13 AM

I've solved it! For some reason, Box Collider 2D doesn't work with Raycast, so putting a normal Box Collider on the object fixed the problem. Thanks for your help.

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 blueshark- · Feb 05, 2019 at 07:29 PM

Works perfectly for me. Are you sure your object you clicked on is named "A"?

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 YuBen · Feb 05, 2019 at 07:41 PM 0
Share

Yes, the object is named A. However, this script is attached onto the "A" object; does that make a difference?

avatar image
0

Answer by Ymrasu · Feb 05, 2019 at 10:46 PM

Physics.Raycast returns the first thing it hits. If you happen to have anything in front of 'A' or if your B, C, and D all have box colliders as well, they could be the ones getting hit.

Try Physics.RaycastAll:

 public IEnumerator Qone()
     {
 
         //t.text = "A: 1";
         while (i == 0) {
             if (Input.GetMouseButtonDown(0)) {
 
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 RaycastHit[] hits = Physics.RaycastAll(ray);
 
                 foreach(var hit in hits) { 
                     if (hit.transform.name == "A") {
                         Debug.Log("hit");
                         i = 1;
                     }
                 }
             }
             Debug.Log("F");
             yield return null;
         }
 
         i = 0;
         StopAllCoroutines();
     }
Comment
Add comment · Show 3 · 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 YuBen · Feb 06, 2019 at 01:57 AM 0
Share

@Ymrasu Thanks for responding. However, I tried using this method and sadly the result was the same as before. I even tried making another scene using this code and just one object, but it still did not work. Do you think it might be a problem with my editor?

avatar image blueshark- YuBen · Feb 06, 2019 at 01:57 PM 0
Share

Is this how you want the object to behave? Behaviour GIF

avatar image YuBen blueshark- · Feb 06, 2019 at 11:17 PM 0
Share

@xBlackhawk Yes, the only difference is that I'm using Unity2d.

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

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

Related Questions

Problem with raycast vertical 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

"Operator `??' cannot be applied to operands of type `bool' and `bool'" Error with C# 6's "??" operator 1 Answer

RayCast2d Not colliding with objects in other layers 1 Answer

Ray is not being detected 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