add collider to cloth component in run time via script

Hi, I want to dynamically add sphere collider to my cloth object after instantiating my projectiles.
but it doesn’t work. and collision doesn’t occur. in fact the ClothSphereColliderPair Array is initialized but the value that I assigned to it, has a problem.

Cloth a = GetComponent<Cloth>();
a.sphereColliders = new ClothSphereColliderPair[1];

GameObject Projectile = Instantiate(_Prefab,Vector3.zero,Quaternion.identity) as GameObject;

 a.sphereColliders[0] = new ClothSphereColliderPair((SphereCollider) Projectile.GetComponent<Collider>());

so what is the problem? the Cloth component after this code is something like this you see that collider variables are none.:

Well, the same problem as everytime ^^. “sphereColliders” is a property. When you assign an array to that property you don’t “set” a variable to your array but you pass the array to the set method. The native Unity engine core will copy that information internally. So changing the array after you assigned it will have no effect on the actual internal data. The same happens in this line:

a.sphereColliders[0] = ....;

Here you don’t invoke the setter of the property but only the getter. The getter will create a new managed array and fill it with the data that is stored internally. Again, changing that array won’t have any effect until you actually assign it to the setter. So you have to do something like this:

Cloth a = GetComponent<Cloth>();
GameObject Projectile = Instantiate(_Prefab,Vector3.zero,Quaternion.identity) as GameObject;

var colliders = new ClothSphereColliderPair[1];
colliders[0] = new ClothSphereColliderPair(Projectile.GetComponent<SphereCollider>());

a.sphereColliders = colliders; // invoke the setter with the initialized array.

The same procedure has to be used for Renderer.materials, Mesh.triangles/.vertices/.colors/…, AnimationClip.events, AnimationCurve.keys, ParticleEmitter.particles and others as well. Basically any array property in a built-in component has to be treated like this.