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);
}
}
}
}