Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Help Wanted Instantiating models at runtime.. do I need to create a separate prefab?

Discussion in 'Editor & General Support' started by Buretto, Apr 4, 2020.

  1. Buretto

    Buretto

    Joined:
    Mar 23, 2015
    Posts:
    42
    IIRC, when a model is imported into unity, the AssetPostProcessor (or something like that) will produce a GameObject prefab with the related Mesh components which can then be dragged into the scene (i.e. the imported mesh can be used as a prefab). Can I instantiate this prefab at runtime? I'm unsure if it is intended for such use cases as it throws a bunch of errors related to the generated MeshCollider when a collision is performed against it. Am I supposed to create a separate GameObject prefab to instantiate? Or is this supposed to work?

    I noticed that the instantiated GameObject's MeshFilter component has the "instanced" suffix and references to a instanced mesh, but it's MeshCollider component references to the non-instanced, actual imported mesh. Not sure if that is related to what's causing this, but ideally it would not create a new mesh instance when I Instantiate it into the scene as I don't modify it or anything; is that possible? (perhaps Instantiate is the wrong function I should be using).

    The only thing I change to the mesh GameObject once it is Instantiated is add one MonoBehaviour script, so hopefully I can get away with using the imported prefab as creating a separate prefab for each unique mesh would feel redundant.

    I guess I should mention I am importing lots of .fbx and .obj files.

    At the moment I have tried creating a system for automating the prefab creation on import, but that is by itself running into some issues so I thought I would ask here and hopefully it will not be necessary.

    What should I do?
     
    Last edited: Apr 4, 2020
  2. Buretto

    Buretto

    Joined:
    Mar 23, 2015
    Posts:
    42
    The errors turned out to be unrelated and thankfully I am able to use just the imports. Everything is working great.
     
unityunity