1. Submit bugs to enter our 5.4 Beta Sweepstakes. Read more about it here.
  2. Unity 5.4 public beta is now available for all users. Grab the build and visit the 5.4 beta forum.
  3. Missing a module? More about the new installer in 5.3 here.
  4. 4.7.1 released for Xcode 7.3 support. Thread here.

texture at runtime: how to create and attach it

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

  1. giancamati

    giancamati

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

    Joined:
    Jul 18, 2010
    Messages:
    4,151
  3. giancamati

    giancamati

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

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

    Dreamora

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

    andeeeee

    Joined:
    Jul 19, 2005
    Messages:
    8,769
    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

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

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

    giancamati

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

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