Adding Colliders to a Cloth component using script

Hi all. I’m trying to add my player collider to a cloth component using C# script.

CapsuleCollider playerCollider = GameObject.Find("GameManager").GetComponent<GameManager>().player.GetComponent<CapsuleCollider>();
Debug.Log("Add the collider: " + playerCollider);
GetComponent<Cloth>().capsuleColliders[0] = playerCollider;
Debug.Log("This collider has been added: " + GetComponent<Cloth>().capsuleColliders[0]);

The first debug confirms that my players collider is returned successfully, but for some reason it’s not being added to the capsuleColliders array, as the second debug returns blank; and checking the properties of the component confirms nothing is added.1

I have added a size of 1 to the capsule colliders elements in the Cloth component properties, and just left it blank. That is where I am trying to add my players capsule collider.

Does anyone have any idea why I might not be able to add colliders this way?

Bump

Try like this:

CapsuleCollider cc = ToTest.GetComponent<CapsuleCollider>();
           
if (cc != null)
{
   CapsuleCollider[] caps = new CapsuleCollider[1];
   caps[0] = cc;
   print("Have cc.");
   GetComponent<Cloth>().capsuleColliders = caps;
}
else print("Didn't get cc.");
2 Likes