Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by erfan · Apr 19, 2016 at 06:06 PM · collisioncolliderscript.cloth

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.:

alt text

capture.png (27.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Bunny83 · Apr 20, 2016 at 12:31 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cloth Component - Detect Object Colliders and add to Cloth Colliders 0 Answers

OnTriggerEnter2D acting like OnTriggerStay2D 0 Answers

Collision with cloth not working,Cloth component not working 0 Answers

How do I add a box collider to a cloth? 2 Answers

Help with getting script of another player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges