Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to set Box collider size and offset correctly?

Discussion in '2D' started by Babster, Jun 15, 2020.

  1. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    I've created a button and attached it to the canvas inside the scene. I also added the BoxCollider2D component to the button. Then I added the script and bound it to the button. (See screen 1).
    The text of the script is:

    using UnityEngine;

    public class SkipButton : MonoBehaviour
    {

    // Start is called before the first frame update
    void Start()
    {
    Debug.Log("Button started");
    }

    void OnMouseDown()
    {
    Debug.Log("Mouse down");
    }

    }


    Then I start debugging and click the button, nothing happens (see screen 2).

    When I dramatically increase the size of Box Collider, the event fires (see screen 3).

    What am I doing wrong?
    I would appreciate any advice or link to the articles or books related to my issue. Thanks!
     

    Attached Files:

    Last edited: Jun 16, 2020
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    First of all, is there a reason you're not using the inbuilt button click function and are trying to bypass it? I suspect what's going on is the two are interfering.

    I should also note, this sounds much more appropriate to the UI forum than here.
     
    Babster likes this.
  3. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    I want to implement two different functionalities which will differ by the duration of the button pressed.
    Maybe I should use another component for this?
     
  4. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    You should just use the standard button. You shouldn't have to manually edit the collider to get it working. You don't even have to add the collider yourself.

    Also, please use code tags when posting code around here. If you notice, blindly posting code into your post causes it to loose formatting. And, @Derekloffin is correct. This post really belongs in the UI section.
     
  5. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    thanks for your reply. code tag added, sorry for the inconvenience, I'm a beginner.
     
    spryx likes this.
  6. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    no problem, were you able to get the button working?
     
  7. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    Unfortunately wasn't. Tried different approaches, including the deletion of the collider. Click works, MouseDown - not.
    My next ideas are
    1. In videos and text guides sizes of colliders are smaller than in my scene - 1-2 against 150 points. Maybe I can decrease the scene size. Maybe it can help.
    2. If not, I'm going to use sprites instead of buttons.
     
  8. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    Is your OnMouseDown method on a script attached to the same gameobject with the collider? If not, I don't think it will fire.
     
  9. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    See Screen 3 - if I increase the Collider size the event fires. So I assume that the problem is in the geometry or something around it
     
  10. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    Does backimage have a collider? It might just be that another collider is getting the event until you resize the button collider enough that it gets it. I think you have overlapping colliders somewhere.
     
  11. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    It hasn't.
    Also, to check your idea I've added next code to the script

        void Update()
    {

    if (Input.GetMouseButtonDown (0)) {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit)) {
    Debug.Log ("Name = " + hit.collider.name);
    Debug.Log ("Tag = " + hit.collider.tag);
    Debug.Log ("Hit Point = " + hit.point);
    Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
    Debug.Log ("--------------");
    }
    else
    {
    Debug.Log ("nothing caught");
    }
    }
    }


    when I click left mouse button anywhere only "nothing caught" repeats in the debug window
     
  12. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    Created a new project, added a scene, a button, a script - sam result. Added Collider - same result.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    This is 3D physics not 2D physics. Use OverlapPoint to detect if a world-position overlaps a 2D Collider.
     
    Babster likes this.
  14. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    Maybe you can also give me a hint on my main problem, please?
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I don't have anything to add beyond what has already been said. You should try modifying your test above though to use 2D physics if that's what you're using.