Search Unity

Adding Colliders to a Cloth component using script

Discussion in 'Scripting' started by Comafly, Jan 16, 2018.

  1. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Hi all. I'm trying to add my player collider to a cloth component using C# script.

    Code (csharp):
    1. CapsuleCollider playerCollider = GameObject.Find("GameManager").GetComponent<GameManager>().player.GetComponent<CapsuleCollider>();
    2. Debug.Log("Add the collider: " + playerCollider);
    3. GetComponent<Cloth>().capsuleColliders[0] = playerCollider;
    4. 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?
     
  2. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Try like this:
    Code (csharp):
    1.  
    2. CapsuleCollider cc = ToTest.GetComponent<CapsuleCollider>();
    3.            
    4. if (cc != null)
    5. {
    6.    CapsuleCollider[] caps = new CapsuleCollider[1];
    7.    caps[0] = cc;
    8.    print("Have cc.");
    9.    GetComponent<Cloth>().capsuleColliders = caps;
    10. }
    11. else print("Didn't get cc.");
    12.  
     
    dobromircho likes this.