Author Topic: Get the name of UIButton's parent which is clicked.  (Read 3157 times)

abhaya.agrawal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Get the name of UIButton's parent which is clicked.
« on: May 25, 2015, 05:04:52 AM »
So I am adding buttons to scroll view dynamically inside grid and storing all those inside a list. buttons are inside a prefab. in Prefab there are two buttons.

suppose i added 5 prefab inside grid. When i click on any button, I want to get the name of its parent.

I am using raycast hit to get the name. is there any other way to get the name.

this is my code.

void GetParentName()// i am calling this function when clicking on or off button.
    {
        if (Input.GetMouseButtonUp(0))
        {
            Ray ray = cameraUI.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit ,1000.0f))
            {
                Debug.Log("i am inside raycast");
                if (hit.collider.gameObject.name == "ON_Button"
                    || hit.collider.gameObject.name == "OFF_Button")
                {
                    parentNum = int.Parse(hit.collider.gameObject.transform.parent.name);
                    Debug.Log(parentNum);
                }
            }
        }
    }
« Last Edit: May 25, 2015, 05:36:18 AM by abhaya.agrawal »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get the name of UIButton's parent which is clicked.
« Reply #1 on: May 26, 2015, 03:04:16 PM »
What's the point of Input.GetMouseButtonUp? If you are calling this function from OnClick, then you are already doing it. Furthermore you can check UICamera.currentTouchID to see which button it is if that's what you were trying to do. Lastly, why are you doing your own raycast? UICamera.currentTouch has all kinds of information including what was hit. UICamera.lastHit has all the info of the raycast itself.

What you should be doing instead is using the On Click callback part of your buttons. Set them to call a specific function when clicked. This function can reside in an entirely different script if you prefer -- it doesn't matter. There is no need to do any of what you're doing. I suggest you re-examine your approach.

abhaya.agrawal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Get the name of UIButton's parent which is clicked.
« Reply #2 on: May 29, 2015, 04:42:27 AM »
Thanx alot :) you are right my approach is wrong. btw UICamera.lasthi is working superfine with me. thanx alot. :)

abhaya.agrawal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Get the name of UIButton's parent which is clicked.
« Reply #3 on: June 08, 2015, 07:09:21 AM »
Hey..

When I am using UICamera.lastHit.collider.gameObject.name its remembering that and even if I am not clicking on that button, I am clicking some where outside , its calling that function. what can I do to ignore this problem???

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get the name of UIButton's parent which is clicked.
« Reply #4 on: June 09, 2015, 01:19:00 AM »
UICamera.currentTouch gives you all the relevant information about what you're interacting with. UICamera.lastHit simply stores the data of the last successful raycast.

abhaya.agrawal

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Get the name of UIButton's parent which is clicked.
« Reply #5 on: June 09, 2015, 05:56:58 AM »
When I am using

 if (UICamera.currentTouch.current.gameObject.name == "appliance")

i am getting null reference error in unity=> NullReferenceException: Object reference not set to an instance of an object

i want to get name of the object, which i am clicking or touching.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get the name of UIButton's parent which is clicked.
« Reply #6 on: June 11, 2015, 06:10:25 AM »
Add some null checks. UICamera.currentTouch.current is not guaranteed to be something at all times. If you hover over nothing, it will be null.