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 /
This question was closed Jan 08, 2016 at 03:14 AM by usalalas for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by usalalas · Dec 15, 2015 at 12:12 AM · androidscript.buttons

Not Hitting a button with raycast

Good Evening,

I'm developing a game for android. I have several UI buttons and I'm already checking if the button is been touched. The button has a BoxCollider2D, and every time I touch the button it recognize the touch, but the Physics.Raycast is always false. The Script is attached to the Main Camera. Does anyone can help?

 using UnityEngine;
 using System.Collections;
 
 public class ButtonController : MonoBehaviour {
 
     private GameObject objectHit;
 
     // Use this for initialization
     void Start () {
 
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
             Ray buttonRay = Camera.main.ScreenPointToRay(Input.touches[0].position);
             RaycastHit buttonHit;
             if (Physics.Raycast(buttonRay,out buttonHit)){
                 objectHit = buttonHit.transform.gameObject;
                 objectHit.SendMessage("OnClick",SendMessageOptions.DontRequireReceiver);
             }
                     }
     
     }
 }
 

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 usalalas · Dec 15, 2015 at 01:29 AM 0
Share

I believe that there is a problem with the position because of the Rect Transform Position of the button and the position of the touch.

avatar image usalalas · Dec 15, 2015 at 03:01 AM 0
Share

Also, my game is a 2D game.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by usalalas · Dec 17, 2015 at 08:08 PM

If found an answer. This script is attached to the main camera.

 RaycastHit2D buttonHit;
             buttonHit = Physics2D.Raycast(this.transform.position, Input.touches[0].position);
             if (buttonHit)
             {
                 objectHit = buttonHit.transform.gameObject;
                 objectHit.SendMessage("LoadScene",1,SendMessageOptions.DontRequireReceiver);
             }
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 Susajin · Dec 15, 2015 at 11:05 AM

Are you using a canvas? Make sure that the Raycast Target option in Image Component of the Button is checked. But why are you using Raycasting for button clicks? You can just use the OnClick() Listener in the Button Component to call methods you want.

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 usalalas · Dec 15, 2015 at 01:41 PM 0
Share

Good $$anonymous$$orning @Susajin,

The OnClick method is not been called when I touch the screen over the button. The script recognize the touch but doesn't know witch object was touched.

Follow this Question

Answers Answers and Comments

39 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

Related Questions

Android UI push Multiple buttons Help 0 Answers

The UI Button Hitbox changes after using a Input Field on Android 1 Answer

Game working in pc but not in Android While Testing 0 Answers

How to change this script so that I can swipe up to jump and swipe down to come down on a touch device? 0 Answers

My UI Buttons work in editor, but not when I export to android (using Cardboard VR)?,My buttons working on editor, but not on android (Gooogle Cardboard VR) 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