texture at runtime: how to create and attach it

Discussion in 'Scripting' started by giancamati, Nov 17, 2010.

  1. giancamati

    giancamati

    New Member

    Joined:
    Aug 4, 2010
    Messages:
    513
    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
  2. KelsoMRK

    KelsoMRK

    Member

    Joined:
    Jul 18, 2010
    Messages:
    2,773
  3. giancamati

    giancamati

    New Member

    Joined:
    Aug 4, 2010
    Messages:
    513
    Well it's weird because the script I attached at runtimes the Start function is:

    renderer.material.color = Color.red;

    and nothing happens :(
  4. KelsoMRK

    KelsoMRK

    Member

    Joined:
    Jul 18, 2010
    Messages:
    2,773
    Is there a Mesh Renderer component on your GameObject?
  5. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    then you are on the wrong renderer or a material that does not use colors
  6. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    As you say, the material is not actually a component. If your object is a Unity prefab then you can change its texture using:-
    Code (csharp):
    1. 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.
  7. giancamati

    giancamati

    New Member

    Joined:
    Aug 4, 2010
    Messages:
    513
    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 :(
  8. KelsoMRK

    KelsoMRK

    Member

    Joined:
    Jul 18, 2010
    Messages:
    2,773
    How are you calling the script if it's not attached to the GameObject you're trying to manipulate?
  9. giancamati

    giancamati

    New Member

    Joined:
    Aug 4, 2010
    Messages:
    513
    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?
  10. giancamati

    giancamati

    New Member

    Joined:
    Aug 4, 2010
    Messages:
    513
    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.