I'm doing a project in Unity engine and have 3 functions in a C# class, which are structured the following way:
public Vector3 FunctionOne(Vector3 point)
{
// do some vector operations and return it as var a
return a;
}
public Vector3 Sample(System.Func<Vector3, Vector3> funcOne, Vector3 samplePoint)
{
// sample the resulting vector field from funcOne and return as var b
return b;
}
public Vector3 Trace(Vector3 point)
{
// Even more vector operations, as follows
Vector3 k1 = Sample(FunctionOne(point), point);
// (...)
return k1;
}
Basically I need the Trace()
function to call the Sample()
function on some variable, but also need the Sample()
function to take in different functions as a variable, such as for instance FunctionOne()
. I get the following error inside Trace()
when doing it like above:
Argument 1: cannot convert from 'UnityEngine.Vector3' to 'System.Func<UnityEngine.Vector3, UnityEngine.Vector3>'
How would I deal with this properly? As far as I can see, the Sample()
function both takes in and returns a Vector3, I specify what function for it to take in as well so why doesn't this work? Do I need to pass FunctionOne()
to Trace()
as well, even though it's within the same class?
I left out most of the meat of the functions as it's really just vector operations and kind of irrelevant to the question.
This?
public Vector3 Sample(System.Func<Vector3, Vector3> funcOne, Vector3 samplePoint)
{
var b = funcOne(samplePoint);
return b;
}
public Vector3 Trace(Vector3 point)
{
// Even more vector operations, as follows
Vector3 k1 = Sample(FunctionOne, point);
// (...)
return k1;
}