Search Unity

How to add MeshCollider to children of Gameobject?

Discussion in 'Scripting' started by pitbrawlzant, Nov 22, 2017.

  1. pitbrawlzant

    pitbrawlzant

    Joined:
    Sep 17, 2013
    Posts:
    3
    I am trying to create a Unity based tool for my degree project in college and have no coding experience so please bear with my ignorance. I am importing geometry into my tool at runtime and need to add a MeshCollider to all of the nested GameObjects and then attach the MeshFilter to the new MeshCollider. I Have tried modifying code I have found elsewhere to no avail. Attached are images of before runtime and after importing during runtime. Thank you.


    Pre Runtime.PNG
    Pre-runtime

    Post Runtime.PNG Post-import during runtime
     
    Last edited: Nov 22, 2017
  2. pitbrawlzant

    pitbrawlzant

    Joined:
    Sep 17, 2013
    Posts:
    3
    Here is what my code is right now for adding the MeshCollider, but it give this message on line 9:

    'obj' is a variable but is used like a type

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewMeshCollider : MonoBehaviour
    4. {
    5.     void OnEnable()
    6.     {
    7.         GameObject[] obj = GameObject.FindGameObjectsWithTag("Geometry");
    8.         obj.GetComponentsInChildren<GameObject>[];
    9.             foreach (GameObject geo in obj)
    10.                 geo.AddComponent<MeshCollider>();
    11.     }
    12. }
     
    Last edited: Nov 22, 2017
  3. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    You can use a foreach loop to iterate through all childobjects of your Geometry object, get it's Mesh and then create a MeshCollider for it. Use the code below on your Geometry object:

    Code (CSharp):
    1. public class ColliderExample : MonoBehaviour {
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.  
    6.         // Iterate through all child objects of our Geometry object
    7.         foreach (Transform childObject in transform)
    8.         {
    9.             // First we get the Mesh attached to the child object
    10.             Mesh mesh = childObject.gameObject.GetComponent<MeshFilter>().mesh;
    11.  
    12.             // If we've found a mesh we can use it to add a collider
    13.             if (mesh != null)
    14.             {                      
    15.                 // Add a new MeshCollider to the child object
    16.                 MeshCollider meshCollider = childObject.gameObject.AddComponent<MeshCollider>();
    17.  
    18.                 // Finaly we set the Mesh in the MeshCollider
    19.                 meshCollider.sharedMesh = mesh;
    20.             }
    21.         }
    22.     }
    23. }
     
    Last edited: Nov 23, 2017
  4. pitbrawlzant

    pitbrawlzant

    Joined:
    Sep 17, 2013
    Posts:
    3
    THANK YOU SO MUCH!!!!!!! I owe you a beer or something!
     
  5. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Glad I could help :D
    I've fixed a small bug in the code on line 13 (removed the brackets). The null check didn't do nothing. You should add the change to your code if you want to make sure your childobject has a MeshFilter.