Search Unity

Attach Mesh Collider via Script at Runtime

Discussion in 'Scripting' started by S_P_S, Feb 25, 2015.

  1. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Hey folks!

    My goal is to add Mesh Collider to a random Object (The user can select it from his pc) and all child objects which have a mesh renderer attached.

    How can I accomplish this?

    I am thankful for any ideas.
     
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Check out the AddComponent method in the Scripting docs. That's what you're going to want to use.

    However, be warned that using a mesh collider is pretty expensive, so use sparingly. I've found that using a series of primitive colliders works well.
     
  3. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Thanks for your answer. I have checked the AddComponent method before but my problem is that I always have to attach a specific gameobject.
    But when I am building my game I don't have this specific gameobject because the user should load the model in runtime. Also I cant attach the mesh collider to a parent. And I dont know how many mesh renderers the model has.
     
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    It sounds like you're trying to focus on learning too much at once. I'd back up and try to tackle the simpler stuff first and come back to this issue when you've got a solid understanding of parenting and components and how they all can interact. :)
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Then the model the user loads *is* your specific GameObject. You can use GetComponentsInChildren<MeshRenderer>() to get all of the child mesh renderers.
     
  6. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Okay thanks for your answers, I think I am now closer to my goal.

    One Question:

    With

    Code (CSharp):
    1. myGameobject.AddComponent<MeshCollider>();
    I can add the Meshcolliders to my Parent gameobject. But I want to add the MeshColliders to the children, not to the parent. Or the best Option is to add the mesh colliders to al children which contain a mesh renderer.
    (Maybe some child Objects have also children and I also want to add a mesh collider to them)

    How ca I do this?

    Thanks for your answers!
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  8. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Thank you for all you answers!

    I got it now.
     
  9. kuldeepsinghthakur

    kuldeepsinghthakur

    Joined:
    Jul 10, 2018
    Posts:
    2
    Is it possible to attach collider at all location in .obj where material is changed
     
  10. kfidanci

    kfidanci

    Joined:
    Jul 11, 2018
    Posts:
    3
    myGameobject.AddComponent<MeshCollider>();

    how can i add this with CONVEX is ON ?

    thank you!!!!!!!!
     
  11. Anil5554u

    Anil5554u

    Joined:
    Aug 14, 2018
    Posts:
    1
    myGameobject.AddComponent<MeshCollider>().convex = true;
     
  12. jeffdyerSRTMarine

    jeffdyerSRTMarine

    Joined:
    Jan 23, 2019
    Posts:
    12
    MeshCollider mc = myGameobject.AddComponent<MeshCollider>();
    mc.convex = true;
    mc.this = that; etc....
     
  13. unity_5F3233DDF34AF1474DBC

    unity_5F3233DDF34AF1474DBC

    Joined:
    Dec 11, 2021
    Posts:
    18
    I have a question that is somewhat similar to this, I added a mesh collider to my gameobject using the AddComponent which is mentioned above and as I inspect it in the hierarchy the mesh collider was added but in the mesh section it is empty. the child of that gameobject tho contains the mesh. what i want to do is assign the child of the gameobject in the mesh field. how can i do that in script
     

    Attached Files:

  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Put the AddComponent code on the child object instead?
     
    smortBlob likes this.