Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

New Cloth Component - Add collider at runtime?

Discussion in 'Unity 5 Pre-order Beta' started by w00dn, Feb 11, 2015.

  1. w00dn

    w00dn

    Joined:
    Apr 28, 2010
    Posts:
    275
    Whenever I add a collider to a cloth component via scripting, it doesn't seem to accept it and is not showing up in the collider array on the cloth component. It only works if i drag it on there in the editor.

    Am i doing something wrong or is this a known issue and just not possible at the moment?

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class ClothTest : MonoBehaviour {
    5.  
    6.     private void Start() {
    7.         Cloth cloth = GetComponent<Cloth>();
    8.         cloth.capsuleColliders = new CapsuleCollider[1];
    9.         cloth.capsuleColliders[0] = GameObject.Find("Player").GetComponent<CapsuleCollider>();
    10.     }
    11. }
    No errors are thrown. The GameObject has the cloth component along with this script. Also the player has a capsule collider.

    Using Unity 5.0.0f1.
     
  2. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    287
    That should be:

    Code (csharp):
    1. var tmp = new CapsuleCollider[1];
    2. tmp[0] = GameObject.Find("Player").GetComponent<CapsuleCollider>();
    3. cloth.capsuleColliders = tmp;
    Unity APIs generally pass arrays by value.
     
    twobob and HenryChinaski like this.
  3. w00dn

    w00dn

    Joined:
    Apr 28, 2010
    Posts:
    275
    Thanks! I seem to have forgotten that :p