-1

I am trying to determine what component is being used as a parameter but I have no clue how to try access it as everything throws an error.

This first function AttemptMove detects what the raycast collides with. It then calls the OnCantMove function and uses the T as the parameter.

protected virtual void AttemptMove<T> (int xDir, int yDir) where T: Component
{
    RaycastHit2D hit; 
    bool canMove= Move (xDir, yDir, out hit);
    if (hit.transform == null)
        return;

    T hitComponent = hit.transform.GetComponent<T> ();
    //Debug.Log (hit.transform.GetComponent<T> ());
    if (!canMove && hitComponent != null)
        OnCantMove (hitComponent); 
}

protected override void OnCantMove<T>(T component)
{       
    Wall hitWall = component as Wall;
    hitWall.DamageWall (wallDamage);
    animator.SetTrigger ("PlayerChop");
}

I want to be able to damage the enemy player the same way I damage the wall so I need to be able to do some sort of check in the OnCantMove function to determine what T is. I am quite unfamiliar with the generic functions in unity so any help will be appreciated.

Damian
  • 19
  • 6
  • 1
    What exactly is the error? – Braedon Wooding Mar 13 '17 at 01:58
  • 2
    `Wall hitWall = component as Wall;` why? dont you use something like `protected override void OnCantMove(T component) where T: Wall`, [msdn](https://msdn.microsoft.com/en-us/library/bb384067.aspx) -- on a second thought, you had this approach.. well, just saying, casting with `as` might return null, and it will be fun. – Bagus Tesa Mar 13 '17 at 01:59
  • Well I tried to check what the `component.transform.tag` would debug but it showed me nothing in the console – Damian Mar 13 '17 at 02:26

1 Answers1

0

You can check T type using

 Type itemType = typeof(T);
 if(itemType == typeof(int) || itemType == typeof(decimal))
Rao
  • 19
  • 4
  • : Error CS0246: The type or namespace name 'Type' could not be found (are you missing a using directive or an assembly reference?) – Damian Mar 13 '17 at 08:49
  • please refer - https://msdn.microsoft.com/en-us/library/system.type(v=vs.110).aspx – Rao Mar 13 '17 at 10:59