Hi guys,
Could any one clarify my question below, to me, it looks unnecessary to do 2 foreach loops, at lease in this situation. I cann’t think of a scenario of an object has more than 1 scripts implementing IRaycastable interface. For instance, a hitObj can be either a combat, a pickup or a movement obj, once a cursor hover upon it, it will only show one cursor type because they all exclusive to another. I could not understand why we need an inner foreach loop to get all raycastComponents in one hitObj. Please can anyone assist with my question? Thank you.
bool InteractWithRaycastableComponent()
{
RaycastHit[] hitObjs = Physics.RaycastAll(GetMouseRay(), maxRayDistance);
foreach (RaycastHit hitObj in hitObjs)
{
IRaycastable[] raycastComponents = hitObj.transform.GetComponents<IRaycastable>();
foreach (IRaycastable raycastComponent in raycastComponents)
{
if (raycastComponent.HandleRaycast(this))
{
ChooseCurser(raycastComponent.GetCursorType());
return true;
}
}
}
}
Could above just simply into 1 foreach loop?
bool InteractWithRaycastableComponent()
{
RaycastHit[] hitObjs = Physics.RaycastAll(GetMouseRay(), maxRayDistance);
foreach (RaycastHit hitObj in hitObjs)
{
IRaycastable raycastComponent= hitObj.transform.GetComponent<IRaycastable>();
if (raycastComponent== null) continue;
if (raycastComponent.HandleRaycast(this))
{
ChooseCurser(raycastComponent.GetCursorType());
}
return true;
}
return false;
}