Unity Community


Results 1 to 10 of 10

  1. Posts
    513

    texture at runtime: how to create and attach it

    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. Location
    USA
    Posts
    2,540
    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/documenta.../Material.html

    Then attach it via renderer.material:
    http://unity3d.com/support/documenta.../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.


  3. Posts
    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. Location
    USA
    Posts
    2,540
    Is there a Mesh Renderer component on your GameObject?


  5. Location
    Zürich, Switzerland
    Posts
    26,572
    then you are on the wrong renderer or a material that does not use colors

  6. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    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:  
    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.
    I'm wired to the world... that's how I... know... everything...


  7. Posts
    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. Location
    USA
    Posts
    2,540
    How are you calling the script if it's not attached to the GameObject you're trying to manipulate?


  9. Posts
    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. Posts
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •