A quite basic question:
Let’s say I have created a prefab with one or more scripts attached (with objects linked to multiple variables), colliders, etc.
How do I change only the geometry used for the prefab? I want to be able to duplicate the prefab and then reuse all the configuration for the script,colliders, etc for a new model. Is it possible or do I have to build the prefab “from scratch” every time?
The same question applies to when I need to modify the geometry used by a prefab. Or, to be specific, the model axis. I noticed one prefab had the Z axis facing the wrong way (the model went backwards ), and when I corrected it in C4D it carried through everywhere EXCEPT in the prefab referencing the model. Do I need to rebuild it from scratch to fix this relatively minor problem?
You can edit the first two levels of any prefab’s hierarchy directly in the Project pane. This include adding/removing components.
If you’re just changing variables, it might be easier to instantiate the prefab, change the variables you want, and then choose GameObject>Upload Changes to Prefab.
I know, but the thing here is that I didn’t start with an empty. Maybe that’s the way to go, but I didn’t.
So, the model I need to change is the very base of my prefab - it has all the stuff (scripts, etc) attached directly to it.
Had I started with an empty, then added model assets, and attached the scripts to the empty this would have been easy. Is that the way I’m supposed to do it, or is the an easy way to replace model assets in a prefab?
Admittedly the question is mostly out of curiosity as I know how to work around it, but I’d like to know how it is “meant to be done”.
first off… duplicate your prefab in case my suggestion is bad.
with your prefab selected in project view, drag your new mesh onto the mesh filter slot. note, i mean the white icon (the mesh) in your model’s hierarchy, not the object with the colored cube icon. don’t know if you have to redo mesh colliders when doing this. i’m guessing not but might be something to check.
i don’t think there’s a “supposed to” way other then whatever works for you.
The reason your prefab’s mesh doesn’t change its orientation is probably because you are changing the object orientation in your modelling program, rather than the local positions of the vertices. In Blender, you can move vertices by selecting the object in Object mode, switching to Edit mode, pushing A for select all, and then R for rotate. This changes the orientation at the lowest level, and will propagate to the mesh rather than just the transform of the mesh.
Nope.
However, I probably didn’t explain it well enough. In C4D, you have the option of rotating the object axis itself. So by rotating the object axis, you indirectly reposition the actual geometry in local space. This carries over to Unity as well, but dosen’t affect the already set prefab (why I don’t know).
Anyway, I’ve now changed the way I set up some of the assets. I’ve added all the setup stuff to an empty gameobject and linked the actual geometry to it. That makes switching geometry much easier.
Rotating the object axis is not the same as rotating the vertices. When you import a model into Unity, both the vertex position information and the object transform are imported. When you update either, the information will be update in Unity as well. This is visible when you click on the imported mesh in your assets pane: the object with the mesh attached will have the same transform as the object in the editor. However, because your prefab only reads the mesh, but has its own transform, changing the object’s rotation in your editor won’t change it in the prefab. For that, you have to move the vertices in object space.
If that’s correct, it explains why rotating the model axis in C4D changed the rotation in Unity - except for the prefab meshes. However, I find the behaviour to be weird since it would be logical that the prefab would read all the data from the original model file. A prefab is only an instance after all.
Anyway, problem solved. I’ve separated the mesh from the actual prefab to make switching of geometry easier.
I just had exactly the same scenario, and I realized what’s happening. Transforms of instantiated prefabs have their own values, and are not linked to their parent prefabs. Instantiating a new copy of the prefab will show you that the new transform information has indeed been re-imported.