Unity Community


Page 1 of 2 12 LastLast
Results 1 to 20 of 25

  1. Posts
    10

    Change GameObject Texture by script

    Hi there

    I'm just starting on Unity. I'm trying to develop a "game" that I need to change the walls textures on runtime. So in the game I have a bunch of cubes (walls) ant the I have on my assets folder the textures and the materials that have those textures mapped.

    I need to, when the user presses a key it changes the textures of a wall.

    I have tried rendered.material.mainTexture = Resources.Load("path/to/texture"). And some variations of this.

    I have tried also to define a shader the apply the shader to the material, like

    var shader = ...;
    renderer.material = new Material(shader);
    With this I was able to change the look of the wall on runtime but I cant load the texture to that shader.

    But with no success, so I would need the change the texture on runtime and the tile(x,y).

    I'm scripting in javascript.

    Thanks for the help


  2. Location
    Celle, Germany
    Posts
    58
    Hello,

    I'm not sure (also a newbie) but I think you should use:

    renderer.material = material;

    Ulrich


  3. Location
    Italy
    Posts
    217
    You can use this command:
    material.SetTexture("_MainTex", Texture)

    where "_MainTex" is the name of the texture
    (from the docs:
    "_MainTex" is the main diffuse texture. This can also be accessed via mainTexture property.
    "_BumpMap" is the normal map.
    "_LightMap" is the lightmap texture.
    "_Cube" is the reflection cubemap.)

    and Texture is the texture object you want to be rendered.


  4. Posts
    10
    I have tried this already, I think the problem is that I can't reference the texture(jpg image) in the assets folder


  5. Location
    Italy
    Posts
    217
    Quote Originally Posted by Uonze
    I have tried this already, I think the problem is that I can't reference the texture(jpg image) in the assets folder
    If you want to dynamically change the texture you need to put it in the Resources folder and load it with Resources.Load command. Then you can use it in the SetTexture instruction


  6. Posts
    10
    I'm already doing that, please look the image in the attachment. As you can see in the image(attachment) there is a wall and in my resources folder I have the materials and the textures folder, each material as mapped one of the textures and I wanted to change the wall material or the wall material texture, in this case I'm trying to change the wall texture that has already assigned the material1.

    This function is defined in the wall1.js script associated to the wall GameObject on stage

    Code:  
    1. function Update ()
    2. {   
    3.     if (Input.GetButton ("ChangeWall1"))
    4.     {
    5.         renderer.material.mainTexture = Resources.Load("_textures/T01", Texture2D)
    6.     }
    7. }

    but this doesn't work, when I press the key assigned to ChangeWall1 the wall turns gray like the texture has been unset.
    Attached Images  


  7. Location
    Italy
    Posts
    217
    Quote Originally Posted by Uonze
    Code:  
    1. function Update ()
    2. {   
    3.     if (Input.GetButton ("ChangeWall1"))
    4.     {
    5.         renderer.material.mainTexture = Resources.Load("_textures/T01", Texture2D)
    6.     }
    7. }
    Try the code:
    Code:  
    1. material.SetTexture("_MainTex", Resources.Load("_textures/T01", Texture));

    Moreover, dont use a Texture2D but a Texture object


  8. Posts
    10
    I don't think that the proble is changing the texture but it is referencing the texture in the assets forlder.

    using this:

    Code:  
    1. function Update ()
    2. {
    3.     if(Input.GetKey ("1") )
    4.     {
    5.         var texture:Texture2D   = Resources.Load("_textures/T01");
    6.         if(texture)
    7.         {
    8.             Debug.Log("Texture Loaded Sucessfully...");
    9.             renderer.material.mainTexture = texture;
    10.         }
    11.         else
    12.         {
    13.             Debug.Log("Unable to Load texture...");
    14.         }
    15.        
    16.     }
    17. }

    I allway keep getting the "Unable to Load texture..." Message, as you can see in the image that I posted earlier I think the path is correct...


  9. Posts
    10
    I figured it out, what a noobie mistake...

    I didn't realise that for you to use Resources.Load, the recource had to be on a Recources folder, I thought it only had to be on the assets folder.

    thanks for the help.


  10. Posts
    175
    try it:

    Code:  
    1.  
    2. function Update ()
    3. {
    4.    if(Input.GetKey ("1") )
    5.    {
    6.      
    7.       if(Texture)
    8.       {
    9.          Debug.Log("Texture Loaded Sucessfully...");
    10.          renderer.material.mainTexture = Texture;
    11.       }
    12.      
    13.      
    14.       else
    15.       {
    16.          Debug.Log("Unable to Load texture...");
    17.       }
    18.      }
    19.     }

    i tested it and it's works great.
    just select the correct texture on the var when you aplly the script at our gameobject.


  11. Location
    California
    Posts
    139
    Quote Originally Posted by devilkkw
    try it:

    Code:  
    1.  
    2. function Update ()
    3. {
    4.    if(Input.GetKey ("1") )
    5.    {
    6.      
    7.       if(Texture)
    8.       {
    9.          Debug.Log("Texture Loaded Sucessfully...");
    10.          renderer.material.mainTexture = Texture;
    11.       }
    12.      
    13.      
    14.       else
    15.       {
    16.          Debug.Log("Unable to Load texture...");
    17.       }
    18.      }
    19.     }

    i tested it and it's works great.
    just select the correct texture on the var when you aplly the script at our gameobject.
    devilkkw, I freakin' love you dude. *fist bump!* I tried what you made for textures. It works flawlessly! Uhhh! I'm so happy. Exactly what I was looking for. Today is a great day.
    ...and may the force be with you


  12. Location
    Johnstown, PA
    Posts
    5

    Example

    Uonze, I am just trying to learn the whole texture swapping thing for a project I am working on. If you have successfully done this now, could you possibly post an example/mini-tutorial on what you did? Or possibly send it to me directly if need be? I have not been able to find a good tutorial/example on how to do this and what the folder structure needs to be to let me swap a texture. Basically for now I want to swap the dirt texture on the ground to a grass texture when a button is clicked.

    If anyone else has an example/tutorial or a link to one I will gladly accept that all as well. Thanks all.
    Your focus determines your reality.


  13. Location
    Germany
    Posts
    55
    Hello Kei,
    try out his one:
    Code:  
    1. public var bildGroesse = 0.1;
    2.  
    3. function Start () {
    4.  
    5.     ///////////////GEOMETRIE/////////////////////////////////////
    6. var bildObject : GameObject = new GameObject.CreatePrimitive(PrimitiveType.Plane);
    7. bildObject.transform.position = Vector3(0, 0.5, 0);
    8. bildObject.transform.Rotate(-90,0,0);
    9. bildObject.transform.localScale.x *= bildGroesse;
    10. bildObject.transform.localScale.z *= bildGroesse; //one =(1,1,1)
    11.  
    12.  
    13. ////////////////////////TEXTURE////////////////////
    14.       {
    15.          Debug.Log("Texture Loaded Sucessfully...");
    16.          bildObject.renderer.material.mainTexture = Texture;
    17.       }
    18.    
    19.         else
    20.       {
    21.          Debug.Log("Unable to Load texture...");
    22.       }
    23.  
    24.  
    25.    
    26. }


  14. Location
    Johnstown, PA
    Posts
    5

    sweet

    rombowich, Thanks for the help. Using the game object made it work for me. Thanks.
    Your focus determines your reality.


  15. Location
    California
    Posts
    2

    Hello everyone! I need help, I have similar problem ...

    Hello everyone! I need help, I have similar problem ...

    I want when press key "1" load the texture (Tex01)... and key "2" to load the texture 2 (Tex02) in Cube01

    Cube01.js/Code:

    function Update ()
    {
    if(Input.GetKey ("1") )
    {
    var texture:Texture2D = Resources.Load("Resources/Tex01");
    if(texture)
    {
    Debug.Log("Texture Loaded Sucessfully...");
    renderer.material.mainTexture = texture;
    }
    else
    {
    Debug.Log("Unable to Load texture...");
    }

    }
    }
    Attached Images  


  16. Location
    Germany
    Posts
    55
    Look at this:
    http://unity3d.com/support/documenta...Resources.html

    just write:
    Code:  
    1. ...var texture:Texture2D = Resources.Load("Tex01");...


  17. Location
    California
    Posts
    2
    Quote Originally Posted by rombowich
    Look at this:
    http://unity3d.com/support/documenta...Resources.html

    just write:
    Code:  
    1. ...var texture:Texture2D = Resources.Load("Tex01");...
    rombowich thanks, really helped my day!!!


  18. Location
    Germany
    Posts
    55
    you are welcome


  19. Posts
    68

    hi

    hi every one...!!!!
    i am doing a project on display screen, in this i going to change the texture as like login, enter password and ID, i making texture for all those , but i have to change the texture of moniter according to user click on the screen

    note: i have many texture along the screen, i have particulary change the display screen alone

    eg: if the user press the next button it have to change to the login deatiles and then have to display the password texture and then to ID texture screen..


    some one pls help me out..


    Thanks
    arun

  20. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    You can show several separate GUI screens using a "state" integer:-
    Code:  
    1. var state: int;
    2.  
    3. function OnGUI() {
    4.     if (state == 0) {
    5.       GUI.DrawTexture(texRect, texture1);
    6.       GUI.Label(labelRect, "Login");
    7.     } else if (state == 1) {
    8.       GUI.DrawTexture(texRect, texture2);
    9.       GUI.Label(labelRect, "Details");
    10.     } // etc...
    11. }
    You just need to change the value of the state variable depending on what the user does on each screen.
    I'm wired to the world... that's how I... know... everything...

Page 1 of 2 12 LastLast

Posting Permissions

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