Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: abhaya.agrawal 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);
}
}
}
}
-
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.
-
Thanx alot :) you are right my approach is wrong. btw UICamera.lasthi is working superfine with me. thanx alot. :)
-
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???
-
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.
-
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.
-
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.