- Home /
Help with my first fbx model in Unity via Script!
All I want to do is make a GameObject variable in my custom class, assign my fbx model to the gameobject variable and be able to render my model to the screen all without using the drag and drop Inspector. I want to do all this in script.
I have some experience in c# and scripting in Unity GUI. I also a played around with 3d models in Unity via the Inspector. What i don't have experience in, is creating gameobjects and assigning 3d models in Unity using scripts only.
For example I have imported a .fbx model in Unity via the Inspector and rendered to the screen and done basic things as rotate it in a script. But i need help loading my model via script and not use the inspector.
I have a fbx model that has 2 meshes and 2 textures. When i drag my model from the assets folder to the Hierarchy it auto creates a parent object that just has a transform with 2 children objects that have my 2 meshes and textures that make up the model. I can render to screen and control all of this via the parent gameobject's transform with no problems.
But if i avoid the drag and drop Inspector method and try to do this in script via myGameObject = Resources.Load("myModel") as GameObject;
i get that error "It has no render" if i try to do anything with myGameObject.
I check to see if it was including the children via:
Debug.Log ("child:" +myGameObject.transform.childCount);
It did as Debug.Log was printing out the value 2. But when I try the render myGameObject in the script I get an error saying "It has no render". Well it's true, it does not have a render, only a transform, but the children do have meshfilter and render.
What confuses me is I don't get this error when I drag and drop my model in the Hierarchy from the assets folder.
In short, Unity is creating GameObjects with $$anonymous$$eshFilters and $$anonymous$$eshRenderers, setting the $$anonymous$$eshFilters, and assigning $$anonymous$$aterials. You can do all the same things in script, but it is far simpler to create Prefabs and just instantiate them as is.
To create a prefab, just drag a Hierarchy object to the Project folder. If its in Resources, it's available via Resources.Load, but you can also assign Prefabs as GameObject variables in the inspector (fyi).
I made my fbx model a prefab and added it to my Resource folder. I'm still getting the error message "It has no render" whenever I try to do something with it such as a transform.
I would like to note that my prefab in the resource folder has a Hierarchy.
In the script i have
GameObject myObject;
void Start ()
{
myObject = Resources.Load("myPrefab") as GameObject;
myObject.transform.position = myVector;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613104548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Triangle Mesh Problem 2 Answers
Why is my model jumping to position 0,0,0? 1 Answer
How to Change Parenting while in game? C# 1 Answer
Polygons, NURBs & Sub-Divisions 2 Answers