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.
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.
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);
}
}
}
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.