x


Importing a model without it becoming a prefab

This may be a stupid question (I don't even know if anyone else has it), but is there a way to import models into the game without them directly turning into prefabs?

Very often I need to add scripts to a model, but I cant, because it's a prefab, and for some reason I can't add scripts to a "model prefab" without breaking the link (and I can't use apply).

This wouldn't seem like a big problem ( i can make a new prefab and drag the broken into that one), but it gets really messy.

As always, any help is appreciated!

more ▼

asked Mar 31 '10 at 03:23 PM

ZanzibarDreams gravatar image

ZanzibarDreams
273 10 12 22

The question is OK.. Just few notes: there is no such thing as a stupid question.. as long as it has to do with Unity.. and there is no need for asking for help.. we are all here to help and get help..!! more in http://answers.unity3d.com/faq Regards

Mar 31 '10 at 03:34 PM Lipis

@Lipis - at least he asked for help in the text - not one giant Title saying HELP!

Mar 31 '10 at 04:07 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

The prefabs you get when you drag in a model are not "real" prefabs. You can see that they have a different icon. The correct procedure is to make an actual prefab the usual way (Create -> Prefab) and drag the model onto that.

more ▼

answered Mar 31 '10 at 04:05 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

That's a shame :D

Apr 01 '10 at 08:57 AM ZanzibarDreams

Hmm...I'm not sure why you think so, since the answer to your question about "is there a way to import models without them turning into prefabs" is basically "they never were actually being turned into prefabs", which seems to be what you wanted in the first place. You can drag the bare mesh into the editor if you don't want the "fake prefab" effect, but then you lose out on things like automatic texture assignment and orientation correction.

Apr 01 '10 at 09:36 AM Eric5h5

Now you're getting it :D

Apr 06 '10 at 08:09 AM ZanzibarDreams
(comments are locked)
10|3000 characters needed characters left

There's one other option here, which is to use the AssetPostprocessor scripting.

With this, you can programmatically add components to your models during the import process (including built-in components such as Colliders, as well as your own scripts).

In this way, you can actually end up with a model with a script attached to it, which you can use right off the bat. :-)

Here's an example C# script which adds an Enemy behaviour to any object imported which contains the word "enemy" in its name:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ModelPostProcessor : AssetPostprocessor {

    void OnPostprocessModel (GameObject g) {
        Apply(g.transform);
    }

    void Apply (Transform transform)
    {
        if (transform.name.ToLower().Contains("enemy"))
        {
            transform.gameObject.AddComponent<Enemy>();
        }

        // Recurse
        foreach (Transform child in transform)
        {
            Apply(child);
        }
    }
}
more ▼

answered Apr 01 '10 at 09:08 AM

duck gravatar image

duck ♦♦
41.4k 95 152 415

(comments are locked)
10|3000 characters needed characters left

I think your problem here is that you're thinking that making a new prefab from the "broken" one is "messy", wheras it's actually a very powerful tool which doesn't need to be messy at all - it's one of the fundamental ways that developing in unity works, and in fact what you're doing is creating a new game-ready functional prefab based on that imported model.

If you're finding it messy, you probably just need to work out a way of organising your original imported 3d assets in your Asset folder so that they are separated from your functional prefabs that you use in your game. You can do this by creating folders in your project pane.

For example, you might have "enemy.fbx" which contains your enemy model. You'd then want to create a functional prefab based on this model. So first, you'd drag the enemy model into your hierarchy (which adds it to the current scene). You'd then add any relevant scripts - such as a script which controls its behaviour, and maybe other separate scripts which might perform global actions like allowing it to show up on a radar, or allowing it to be a valid targetable item, etc.

You'd probably also want to add a collider and perhaps a rigidbody component if you're using the physics engine to move it around. You might also want to add things like a particle system for its engines, or wheelcolliders if it's a wheeled character. It might need an audio source for any sounds that it should emit, etc.

In this way, you should be able to see that you start off with the basic model, and build up a fully functional Game Object by adding components to it.

Now - you create a new prefab, and drag your completed Game Object into this new prefab. This is now the "Enemy" prefab that you should instantiate in your game - either by manually placing them around your levels, or by dynamically instantiating them at runtime via some kind of enemy manager script.

I hope this helps!

more ▼

answered Mar 31 '10 at 03:37 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

I wouldn't have to organize half as much if I could make a prefab right of the bat with assets that I only have one specific use for. cheers for the one page answer though

Apr 01 '10 at 08:56 AM ZanzibarDreams

I've added another answer. This answer still holds (this is the way you're supposed to use prefabs in unity), however there's a editor-scripting method you can use to add scripts directly to models as they are being imported.

Apr 01 '10 at 09:10 AM duck ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1285
x584
x574
x403

asked: Mar 31 '10 at 03:23 PM

Seen: 2767 times

Last Updated: Mar 31 '10 at 03:23 PM