Cannot cast to FreeControllerV3

fabio

Well-known member
Messages
70
Reactions
263
Points
53
I'm working on a script where I need to find all FreeControllerV3 inside a given sphere. I can find the names with the following code

Code:
Collider[] hitColliders = Physics.OverlapSphere(Object.control.position, 0.5f);

foreach (var hitCollider in hitColliders)
{
    if(hitCollider.gameObject.name.Contains("Control"))
    {
        SuperController.LogMessage(hitCollider.gameObject.name);
    }
}

hitCollider.gameObject.name shows common names like "hipControl", "headControl", and so on. Now the problem is how can I cast hitCollider -> FreeControllerV3? No matter whether I try I get a "Cannot implicity convert..." message.
 
FreeControllerV3 is another Behavior on the gameobject. Try doing hitCollider.gameObject.GetComponent<FreeControllerV3>() instead. I can't confirm now, but if this doesn't work it just means the collider is not directly on a FreeControllerV3, but rather in a child object. You can traverse the parents to find it (e.g. hitCollider.transform.parent.gameObject).
 
hitCollider.gameObject.GetComponent<FreeControllerV3>(); works like a charm. Thank you!
 
Back
Top Bottom