I have the code below that works to identify what my mouse was over when done dragging. How do I take that information and make ‘someCell’ the gameObject that was found?
EVEN MORE useful would be if you could tell me how to make it only work on gameObjects that are children of a specific gameObject.
public Cell someCell;
public void OnEndDrag(PointerEventData eventData)
{
if (Physics.Raycast(ray, out hit, 1000f))
{
Debug.Log(hit.transform.gameObject.name + "has been detected.");
}
}
I figured out the ‘child of’ part:
if (Physics.Raycast(ray, out hit, 1000f))
{
if (hit.transform.IsChildOf(field.transform))
{
Debug.Log(hit.transform.gameObject.name + "has been detected.");
}
}
So now I guess my only concern is how do I take that information and make ‘someCell’ equal to it? One extra bit of information - all of the cells in the grid have the exact same name. So using the name alone may not work.
Assigning a variable:
someCell = hit.transform.gameObject;
Checking the parent:
if (hit.transform.parent.gameObject == yourGameObject)
IsChildOf will test for any level of nesting, rather than just direct parentage.
Thanks, Antistone! The parent part works. I made a slight mistake in my original question though.
‘someCell’ isn’t a gameObject. I have it set up as:
public Cell someCell;
How do I make whatever is detected onEndDrag assigned to public Cell someCell?
I think I got it. I added to the answer you gave me, I appreciate it:
someCell = hit.transform.gameObject.GetComponent<Cell>();