Change GameObject Texture by script

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

  1. Uonze

    Uonze

    New Member

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

    New Member

    Joined:
    Jun 8, 2009
    Messages:
    58
    Hello,

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

    renderer.material = material;

    Ulrich
  3. simone007

    simone007

    Member

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

    Uonze

    New Member

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

    Member

    Joined:
    Oct 30, 2008
    Messages:
    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

    New Member

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

    Member

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

    Uonze

    New Member

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

    New Member

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

    New Member

    Joined:
    Jun 24, 2009
    Messages:
    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. r3d5

    r3d5

    Member

    Joined:
    Jan 9, 2010
    Messages:
    140
    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

    New Member

    Joined:
    Apr 12, 2010
    Messages:
    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

    New Member

    Joined:
    Apr 26, 2009
    Messages:
    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

    New Member

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

    joeperry54

    New Member

    Joined:
    Jul 22, 2010
    Messages:
    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

    New Member

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

    joeperry54

    New Member

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

    rombowich

    New Member

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

    Arun_Rajps

    New Member

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

    Unity Technologies

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

    New Member

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

    Thanks
    Arun
  22. Matt_001

    Matt_001

    New Member

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

    nayan2147

    New Member

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

    Torres

    New Member

    Joined:
    Mar 29, 2013
    Messages:
    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

    Member

    Joined:
    Jan 10, 2013
    Messages:
    14
    public Texture newTexture;
    Onstart()
    {
    gameobject1.renderer.material.mainTexture = newTexture;
    }