Hello everybody,
I have an OBJ file which is my 3D model I upload at runtime. Now, I want to attach a Texture as soon as it is loaded up. So, I thought that calling
gObj.AddComponent(Material) would be helpful but apparently I can’t do it as Material doesn’t derive from Component. So, I decided to create a separate script, and call
gObj.AddComponent(MyScriptForTexture);
and modify the Start() function in a way to load the texture and attach it… but I can’t find the way to load a texture from a file. 
Any help or simple example?
Regards,
Giancarlo
You can’t directly add a Material via that method. If you’re talking about creating a Material from scratch via code then check out this:
http://unity3d.com/support/documentation/ScriptReference/Material.html
Then attach it via renderer.material:
http://unity3d.com/support/documentation/ScriptReference/Renderer.html
This will also allow you to manipulate the properties of the material on the fly - such as fading between 2 diffuse colors for example.
Well it’s weird because the script I attached at runtimes the Start function is:
renderer.material.color = Color.red;
and nothing happens 
Is there a Mesh Renderer component on your GameObject?
then you are on the wrong renderer or a material that does not use colors
As you say, the material is not actually a component. If your object is a Unity prefab then you can change its texture using:-
renderer.material.mainTexture = newTexture;
If you are constructing the mesh yourself directly from the OBJ data then you need to add a MeshRenderer component to the object before using the renderer property in code.
Uhm… my OBJ data has a MeshRenderer already. yes. And then… I call a new script, MyScript.js, where in the Awake() I’ve put that
renderer.material.color = Color.red;
if my GameObject is called obj I called
obj.AddComponent(MyScript) where my script is the Standard Asset folder of my project.
Nothing happens 
How are you calling the script if it’s not attached to the GameObject you’re trying to manipulate?
UHm now I’m confused…
the script that allocates my object calls:
gObj.AddComponent(myScript);
in MyScript.js in the Awake() function I call:
function Awake() {
Debug.Log(“Room Texture Loaded”);
renderer.material.color = Color.red;
}
I thought that once the Object is rendered and the call to Awake is automatic. Isn’t it?
Weird… so I moved that line into the Update function and I’ve found that it worked. … I don’t understand why tho.
and moreover as my geometry is a room with detached walls… not all the geometry has been coloured but only the pieces attached to each other.