Hi everybody.
I’ve a little bug with the following script in a Controller class :
/// <summary>
/// Make a raycast from the mouse position to the world.
/// </summary>
/// <typeparam name="U">The type of the component you want to get.</typeparam>
/// <param name="_MaxDistance">The maximum distance of the ray.</param>
/// <param name="_LayerMask">The collision layers you want to collide/ignore.</param>
/// <returns>Returns the component of the given type on the first hit collider.</returns>
public U GetPointedElement<U>(float _MaxDistance = Mathf.Infinity, int _LayerMask = Physics.DefaultRaycastLayers)
where U : Component
{
return GetPointedElement<U>(Input.mousePosition, _MaxDistance, _LayerMask);
}
/// <summary>
/// Make a raycast from the given screen position to the world.
/// </summary>
/// <typeparam name="U">The type of the component you want to get.</typeparam>
/// <param name="_ScreenPosition">The screen position, start point of the ray.</param>
/// <param name="_MaxDistance">The maximum distance of the ray.</param>
/// <param name="_LayerMask">The collision layers you want to collide/ignore.</param>
/// <returns>Returns the component of the given type on the first hit collider.</returns>
public U GetPointedElement<U>(Vector3 _ScreenPosition, float _MaxDistance = Mathf.Infinity, int _LayerMask = Physics.DefaultRaycastLayers)
where U : Component
{
Ray toWorld = Camera.main.ScreenPointToRay(_ScreenPosition);
RaycastHit rayHit;
if(Physics.Raycast(toWorld, out rayHit, _MaxDistance, _LayerMask))
{
return rayHit.collider.GetComponent<U>();
}
return null;
}
The first function is just a shortcut to use Input.mousePosition with the overload of GetPointedElement.
Visual Studio is ok with that, but not Unity…
I’ve an error CS1501 in the Unity’s Console, and it says "No overload for method ‘GetPointedElement’ takes ‘3’ arguments.
But as you can see in the script, there IS an overload with 3 that takes 3 arguments ! If I made an error, I really don’t see it, but else, oes anyone have an idea to avoid this error ?
Edit : the class where I wrote these function is a template with type T. You can see that the functions are templates of type U.
Thanks in advance for your help.
Notes :
- I tried to compile my Controller with these functions in a DLL and add it in Unity. It works without errors, but I don’t want to use DLLs.
- If I comment the first function, I don’t have the error. But, if I want to do something like GetPointedElement(Input.mousePosition), I have an error “Method not found: ‘Controller.GetpointedElement’”.
- It works without any facultative argument. But I prefer use them, to avoid tons of overloads.
- The error is thrown only in template classes.