i want to load a fbx model on the Start() function when my program starts. I load the name of the fbx from a XML and then I put it on a variable called stadio:
var stadio = lstStadiums*.SelectSingleNode(“file”).InnerText;[/B]* after that: stadio2 = new GameObject(“Estadio”); //I create a new GameObject called Estadio in a variable called stadio2.
stadio2 = Resources.Load(stadio, GameObject); //I try to load the fbx (that is in the Resources folder) into stadio2. but after that nothing happens, so I try to add a renderer component: stadio2.AddComponent(“renderer”); but Unity tells me this: Can’t add component because class ‘renderer’ doesn’t exist! so, my problem is that I can´t show the model in the Scene View or the Hierarchy View. please, how I could do the load of the mesh by code? thank you very much! igorlean
hippocoder’s method is the easiest but if you must do it entirely via code - ‘renderer’ is not a valid type, but ‘Renderer’ is. Also, better to use the generic version instead of passing it a string ie: AddComponent(); instead of AddComponent(“Renderer”);
finally I made a prefab from the mesh. After that I made a script with a variable and attach the prefab to the variable, Then, in the Start() function I instanciate the prefab. The code works and the mesh is rendered.
Now I have another question, how I could make the object invisble? I tried to disable the renderer property of the gameobject:
var Estadio1 : GameObject; //the variable where I dragged the prefab
** Estadio1 = Instantiate(Estadio1);**
** Estadio1.renderer.enabled = false;**
but unity tells me:
UnityEngine.MissingComponentException: There is no ‘Renderer’ attached to the “estadio01(Clone)” game object, but a script is trying to access it. You probably need to add a Renderer to the game object “estadio01(Clone)”. Or your script needs to check if the component is attached before using it.
So, my question is if i should add a renderer component to the GameObject. I thought that all GameObjects had all the inherited components attached. How can my Estadio1 GameObject be showed in the screen if it has not a Renderer component attached?
Pretty sure if you create a new GameObject via code with - GameObject g = new GameObject(); - then nothing is attached to it. The Editor will do a lot of that thinking for you but in code you have to do it yourself.