- Home /
OBJ model can only have instance materials?
I have a simple code that changes the material of an object to a material from an array.
public Material[] allMats; //an array of materials
public Transform cubePrefab; //a model with at least 1 child transform
void Start(){
Transform t = Instantiate(cubePrefab, transform.position, Quaternion.identity, transform);
t.name = "TEST";
t = t.GetChild(0);
t.GetComponent<Renderer>().sharedMaterial = allMats[0];
}
However, this code will give two different results depending on whether I use the default Unity Cube, or my own OBJ model from blender. The OBJ is just a quad.
My OBJ will always get assigned an (instance) of the material, and the Unity cube will always get a non instance. I want my custom quad to get a non instance material.
What causes this? What model properties interfere with Unity materials? Is it OBJ format specific?
Answer by Happeloy · Nov 16, 2018 at 08:06 PM
I can't test this myself right now, but it could be worth trying. If you select the obj-file in the project view, you should see some info in the inspector. If you press the tab "Materials", there should be two buttons, "Extract Textures" and "Extract Materials". Try pressing those. You can also select on the dropdown menu if you want to use the embedded materials, or external materials.
I believe this is the answer. By default there's the "Embedded $$anonymous$$aterial" in the obj file, and I believe you can't change that one, hence it will create an instance. If you extract, or change it to the legacy "Use external materials", it will have a separate material from the obj file, one you can modify from code using shared$$anonymous$$aterial. Give it a try.
I also had to go back through my prefabs and rebuild a few of them. When I adjusted my imported OBJ, the change worked for new instances of the OBJ but did not automatically affect my prefab objects that used the OBJ.
...which also means that if the instance of the OBJ that is imported is modified in any way, such as adjusting the hierarchy of children in the instance, it breaks the relationship with the imported OBJ and thus looses the import settings. :: For Clarity :: 1) Import OBJ to project. 2) $$anonymous$$odify import settings to allow external materials. 3) Instantiate object into scene (everything works properly). 4) Expand model to view children, delete a child. 5) OBJ no longer references the import settings (because child was deleted), and will now only have (instance) of materials.
Your answer
Follow this Question
Related Questions
Changing all materials of selection from code 0 Answers
Fuse objects visually when close to each other Shader/ Effect 2 Answers
Renderers with multiple materials 0 Answers
Index array is out of range (following H&S tut) 2 Answers
Swapping out a single material on a SkinnedMeshRenderer at runtime 0 Answers