• Hi Guest!

    We are extremely excited to announce the release of our first Beta1.1 and the first release of our Public AddonKit!
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. For access to the Public AddonKit you must be a Creator tier member. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.
  • Hi Guest!

    VaM2 Resource Categories have now been added to the Hub! For information on posting VaM2 resources and details about VaM2 related changes to our Community Forums, please see our official announcement here.

Cannot cast to FreeControllerV3

fabio

Well-known member
Joined
Jan 30, 2021
Messages
78
Reactions
287
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