I'm trying to use C# generics on a method in order to send a Vector3 (position) as a parameter. I can print the value correctly but can't store it on a local variable. I also can't access its x, y or z attributes separately.
I'm implementing a state machine. My intention is to use generics for allowing sending different parameters to the different states. The value will be sent to an "Enter" method which will be called once on each state every time that state is set to active or current. So for example: the player will be on the DefaultState->something happens->change state to HeroTeleportingState while sending it it's new position
This is my Enter method on the HeroTeleportingState class:
void IState.Enter<T>(T newPosition)
{
//The following line prints the correct value
Debug.Log("move this hero to " + newPosition);
//The following lines don't work
//pos = newPosition
//pos.x = newPosition.x;
}
The method call is just something like: currentlyRunningState.Enter(Vector3.one);
pos = newPosition
gives me the following error on Visual studio (I'm paraphrasing cause my visual studio is in spanish):
Cannot implicitly convert type 'T' to 'UnityEngine.Vector3'
And pos.x = newPosition.x
gives me something like:
'T' does not contain a definition for 'x' and no extension method 'x' accepting a first argument of type 'T' could be found
EDIT: IState interface:
public interface IState
{
void Enter<T>(T value);
void Execute();
void Exit();
}
position = (Vector3)newPosition;
throws the errorCannot convert type 'T' to 'UnityEngine.Vector3'
. Also, I've never heard of instanceof checks, I'm trying to read about it but can't really understand. \$\endgroup\$