Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    221
    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. 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:
    221
    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
     
    shadarnook likes this.
  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:

    guetta18 likes this.
  7. simone007

    simone007

    Joined:
    Oct 30, 2008
    Posts:
    221
    Try the code:
    Code (csharp):
    1.  
    2. material.SetTexture("_MainTex", Resources.Load("_textures/T01", Texture));
    3.  
    Moreover, dont use a Texture2D but a Texture object
     
  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. var Texture:Texture2D;
    2.  
    3. function Update ()
    4. {
    5.    if(Input.GetKey ("1") )
    6.    {
    7.      
    8.       if(Texture)
    9.       {
    10.          Debug.Log("Texture Loaded Sucessfully...");
    11.          renderer.material.mainTexture = Texture;
    12.       }
    13.      
    14.      
    15.       else
    16.       {
    17.          Debug.Log("Unable to Load texture...");
    18.       }
    19.      }
    20.     }
    i tested it and it's works great.
    just select the correct texture on the var when you aplly the script at our gameobject.
     
    NeptosDS likes this.
  11. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    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. var Texture:Texture2D;
    4.  
    5. function Start () {
    6.  
    7.     ///////////////GEOMETRIE/////////////////////////////////////
    8. var bildObject : GameObject = new GameObject.CreatePrimitive(PrimitiveType.Plane);
    9. bildObject.transform.position = Vector3(0, 0.5, 0);
    10. bildObject.transform.Rotate(-90,0,0);
    11. bildObject.transform.localScale.x *= bildGroesse;
    12. bildObject.transform.localScale.z *= bildGroesse; //one =(1,1,1)
    13.  
    14.  
    15. ////////////////////////TEXTURE////////////////////
    16. if(Texture)
    17.       {
    18.          Debug.Log("Texture Loaded Sucessfully...");
    19.          bildObject.renderer.material.mainTexture = Texture;
    20.       }
    21.    
    22.         else
    23.       {
    24.          Debug.Log("Unable to Load texture...");
    25.       }
    26.  
    27.  
    28.    
    29. }
     
  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,768
    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:
    22
    public Texture newTexture;
    Onstart()
    {
    gameobject1.renderer.material.mainTexture = newTexture;
    }
     
  26. danish115

    danish115

    Joined:
    Aug 28, 2014
    Posts:
    47
    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.
     
  29. Kay86X

    Kay86X

    Joined:
    May 9, 2017
    Posts:
    10
    Texture texture = Resources.Load(the file path in the "Resources" folder in your assets folder) as Texture;

    Gameobject wall;

    wall.GetComponent<Renderer>().material.mainTexture = texture;
     
  30. Ksushqa

    Ksushqa

    Joined:
    Jun 14, 2017
    Posts:
    4
    Thanks everybody for your answers. You've made my day. I have had this very newbie problem (as Uonze), damn on me.
     
  31. KoolGamez

    KoolGamez

    Joined:
    Apr 11, 2020
    Posts:
    29
    material.mainTexture = sometexture;

    does this statement mean that the material's main texture property has a copy of sometexture or its reference?? meaning, if I change sometexture afterwards, will it change the main texture too?
     
  32. ogsnuznlr95

    ogsnuznlr95

    Joined:
    Mar 8, 2020
    Posts:
    1
    (I know this question too old, but newbies maybe look for shortcut answer.)
    You can read this code, I use this on my app. You don't have to find any textures from the explorer search. You should just make a material which is keeper a texture inside. And link it on script a material with just "SerializeField". Than you can set your rendered material's texture with them.

    Code (CSharp):
    1.  
    2.  
    3. // I linked some materials before the start section
    4.     [SerializeField] Material mat1;
    5.     [SerializeField] Material mat2;
    6.     [SerializeField] Material mat3;
    7.     [SerializeField] Material mat4;
    8.  
    9. //start
    10.  
    11. //update whatever,
    12.  
    13. // than my code about materials. My shadergraph's main textures name like that "_MainTexture"
    14.    
    15.  
    16. private void CalculatePoints()
    17.     {
    18.         timerSecondControl += Time.deltaTime;
    19.         if (timerSecondControl >= 0.5f)
    20.         {
    21.             timerSecondControl = 0;
    22.             switch (timerSecondControlFlag)
    23.             {
    24.                 case 0:
    25.                     timerSecondControlFlag = 1;
    26.                     lRend.material.SetTexture("_MainTexture", mat1.GetTexture("_MainTexture"));
    27.                     break;
    28.                 case 1:
    29.                     timerSecondControlFlag = 2;
    30.                     lRend.material.SetTexture("_MainTexture", mat2.GetTexture("_MainTexture"));
    31.                     break;
    32.                 case 2:
    33.                     timerSecondControlFlag = 3;
    34.                     lRend.material.SetTexture("_MainTexture", mat3.GetTexture("_MainTexture"));
    35.                     break;
    36.                 case 3:
    37.                     timerSecondControlFlag = 0;
    38.                     lRend.material.SetTexture("_MainTexture", mat4.GetTexture("_MainTexture"));
    39.                     break;
    40.  
    41.                 default:
    42.                     timerSecondControlFlag = 0;
    43.                     break;
    44.             }
    45.         }
    46. //something about my other codes
    47. }