DoSomething part of transform.GetComponent does not work

For a while, I was following Unity Wiki for alternatives but there was no immediate solution. This error still persists: BCE0019 “KeyFunct is not a member of UnityEngine.Transform”. If anyone can tell me a way to solve/get around this error, I will appreciate it.

private var physicsClass : Transform;
private var spriteClass : Transform;

private var curKey : int;

//... in Start ... 

physicsClass = transform.Find("playerPhysics").GetComponent("playerTransform");

spriteClass = transform.Find("playerPhysics/spritePlane").GetComponent("spriteSheet");

//... in Update ...

physicsClass.KeyFunct(curKey); //ERROR

spriteClass.KeyFunct(curKey); // ERROR

The version of GetComponent(string) doesn’t return the type you are looking for. It will return typeof Component which of course doesn’t contain your class functions.

Try using the generic form or type cast the resulting type form GetComponent, generic is better IMHO.

Generic version:

    private var physicsClass : playerTransform;
    private var spriteClass : spriteSheet;
     
    private var curKey : int;
     
    //... in Start ...
     
    physicsClass = transform.Find("playerPhysics").GetComponent.<playerTransform>(); // if found will return typeof(playerTransform)
     
    spriteClass = transform.Find("playerPhysics/spritePlane").GetComponent.<spriteSheet>(); // if found will return typeof(spriteSheet)
     
    //... in Update ...
     
    physicsClass.KeyFunct(curKey); //ERROR
     
    spriteClass.KeyFunct(curKey); // ERROR

If you need access to the transform(i changed the type fields), access it through the gameObject property.