Search Unity

MeshCollider....emptty sharedMesh, why?

Discussion in 'Scripting' started by giancamati, Nov 8, 2010.

  1. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    Hello Unityrs! :)

    I have this problem: I have a model in OBJ format with I load at runtime in my application.
    now, I call AddComponent(MeshCollider) for enabling collision detection. It says that the meshcollider will read the mesh attached to the main object but when enquire the Inspector I can see that the Mesh field is empty....

    why?

    Kind Regards,
    Giancarlo
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    You may need to explicitly assign the sharedMesh property:

    var collider : MeshCollider = target.AddComponent(MeshCollider);
    collider.sharedMesh = target.GetComponent(MeshFilter).sharedMesh;
     
  3. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    Uhm.... this is what I've done:

    gObj.AddComponent(MeshCollider); in Javascript... where gObj is a GameObject and the mesh is built in the OBJ class where I call:

    gs.GetComponent<MeshCollider>().sharedMesh = m;

    where gs[] is an array of GameObjects and m is a Mesh type. Would it be the same?
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Looks right I guess. Just double-check that you're targetting the correct objects and using the correct mesh. Use some Debug.Log calls to validate your variables at run-time.
     
  5. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    Using the inspector when running the application, it looks that the OBJ mesh has a MeshCollider and the sharedMesh points to the uploaded mesh..
    the problem I have is that new model doesn't interact with the plane allocated in the scene.. :( while a pre-allocated cube does.
     
  6. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    Did you try simply use the 'mesh' not 'sharedMesh' ?
     
  7. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    well I also tried to use BoxCollider but apparently any XXXCollider I attached has Extention = (0,0,0). I thought that once I created a collider and I assigned the gameObject, the collider gets dimensioned automatically... :(
     
  8. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    And anyway I just tried with 'mesh' and I get the warning that 'mesh' is obsolete and changed with 'sharedMesh'.
     
  9. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    So, the collider is there. Did you attach stuff like a RigidBody to it?
     
  10. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    Yes I did,

    but I have found out that if the <MeshCollider>().convex = FALSE, well there is no collision :( if it is TRUE, well there is collision but it behaves like a boxCollider :(
     
  11. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    Just for your reference, I use the this method to create mesh and add collider on the fly. (in unity2.6)

    var g:GameObject = new GameObject.CreatePrimitive(PrimitiveType.Cube); // proxy object
    //....
    //....

    var m:Mesh = json2mesh(h); // parse from JSON
    g.GetComponent(MeshFilter).mesh = m;
    g.GetComponent(MeshFilter).mesh.RecalculateBounds(); // Did you add this line after the mesh has been created ??
    //...
    //...

    var c = g.GetComponent(BoxCollider);
    GameObject.Destroy(c);
    g.AddComponent(MeshCollider); // that's all !


    Hope that help.
     
  12. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    Awww apple! thank you very much... I solved in using the MeshCollider and making it Convex, but definitely this is the solution I was looking for! :)

    Many thanks!