1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. Unity 5.6 is now released.
    Dismiss Notice
  3. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  4. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  5. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Change GameObject Texture by script

Discussion in 'Scripting' started by Uonze, Jun 18, 2010.

  1. Uonze

    Uonze

    Joined:
    Jun 18, 2010
    Posts:
    10
    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. Haegar

    Haegar

    Joined:
    Jun 8, 2009
    Posts:
    58
    Hello,

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

    renderer.material = material;

    Ulrich
     
  3. simone007

    simone007

    Joined:
    Oct 30, 2008
    Posts:
    218
    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.
     
    DBar likes this.
  4. Uonze

    Uonze

    Joined:
    Jun 18, 2010
    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. simone007

    simone007

    Joined:
    Oct 30, 2008
    Posts:
    218
    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. Uonze

    Uonze

    Joined:
    Jun 18, 2010
    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 (csharp):
    1.  
    2. function Update ()
    3. {  
    4.     if (Input.GetButton ("ChangeWall1"))
    5.     {
    6.         renderer.material.mainTexture = Resources.Load("_textures/T01", Texture2D);
    7.     }
    8. }
    9.  
    but this doesn't work, when I press the key assigned to ChangeWall1 the wall turns gray like the texture has been unset.
     

    Attached Files:

  7. simone007

    simone007

    Joined:
    Oct 30, 2008
    Posts:
    218
  8. Uonze

    Uonze

    Joined:
    Jun 18, 2010
    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 (csharp):
    1.  
    2.  
    3. function Update ()
    4. {
    5.     if(Input.GetKey ("1") )
    6.     {
    7.         var texture:Texture2D   = Resources.Load("_textures/T01");
    8.         if(texture)
    9.         {
    10.             Debug.Log("Texture Loaded Sucessfully...");
    11.             renderer.material.mainTexture = texture;
    12.         }
    13.         else
    14.         {
    15.             Debug.Log("Unable to Load texture...");
    16.         }
    17.        
    18.     }
    19. }
    20.  
    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. Uonze

    Uonze

    Joined:
    Jun 18, 2010
    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. devilkkw

    devilkkw

    Joined:
    Jun 24, 2009
    Posts:
    175
    try it:

    Code (csharp):
    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. KingSlizzard

    KingSlizzard

    Joined:
    Jan 9, 2010
    Posts:
    212
    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.
     
  12. kei2332th

    kei2332th

    Joined:
    Apr 12, 2010
    Posts:
    5
    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.
     
  13. rombowich

    rombowich

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

    kei2332th

    Joined:
    Apr 12, 2010
    Posts:
    5
    rombowich, Thanks for the help. Using the game object made it work for me. Thanks.
     
  15. joeperry54

    joeperry54

    Joined:
    Jul 22, 2010
    Posts:
    2
    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 Files:

  16. rombowich

    rombowich

    Joined:
    Apr 26, 2009
    Posts:
    55
  17. joeperry54

    joeperry54

    Joined:
    Jul 22, 2010
    Posts:
    2
  18. rombowich

    rombowich

    Joined:
    Apr 26, 2009
    Posts:
    55
    you are welcome ;-)
     
  19. Arun_Rajps

    Arun_Rajps

    Joined:
    Aug 25, 2010
    Posts:
    68
    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. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,769
    You can show several separate GUI screens using a "state" integer:-
    Code (csharp):
    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.
     
  21. Arun_Rajps

    Arun_Rajps

    Joined:
    Aug 25, 2010
    Posts:
    68
    thanks for your help

    Thanks
    Arun
     
  22. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    In case it still could be usefull for you, try using Resources.LoadAssetAtPath() instead of .Load.
     
  23. nayan2147

    nayan2147

    Joined:
    Oct 24, 2011
    Posts:
    34
    I wan to change only material,not texture...how to do this???
    help me?
     
  24. Torres

    Torres

    Joined:
    Mar 29, 2013
    Posts:
    1
    This is all great stuff and all but in my particular situation I absolutely need to use path names dynamically, and from the research I've done you can't do that when using Resources,Load :(

    Any help please?
     
  25. alok-kr-029

    alok-kr-029

    Joined:
    Jan 10, 2013
    Posts:
    20
    public Texture newTexture;
    Onstart()
    {
    gameobject1.renderer.material.mainTexture = newTexture;
    }
     
  26. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    43
    anybody tell me please how to get renderer.material.mainTexture.name in collision enter
     
  27. brutal160

    brutal160

    Joined:
    Feb 6, 2014
    Posts:
    7
    Hi guys i want to use three b2m materials for my object and want to change them when i push a button , i tried devilkww' code it worked for normal textures but not work for b2m materials how can i change them can you please help me.
     
  28. sekai4444

    sekai4444

    Joined:
    Nov 5, 2015
    Posts:
    6
    HI, I have a similar question. I have a 3d character with 6 different face textures. I am doing an animated scene and would like to be able to key the different face textures. So i would need a public variable on the mesh to select the texture and then key.