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

Hello,

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

renderer.material = material;

Ulrich

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.

5 Likes

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

1 Like

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

function Update () 
{   
    if (Input.GetButton ("ChangeWall1")) 
    {
        renderer.material.mainTexture = Resources.Load("_textures/T01", Texture2D); 
    }
}

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

328886--11588--$unity_107.jpg

1 Like

Try the code:

material.SetTexture("_MainTex", Resources.Load("_textures/T01", Texture));

Moreover, dont use a Texture2D but a Texture object

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

using this:

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

    }
}

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...

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.

try it:

var Texture:Texture2D;

function Update () 
{ 
   if(Input.GetKey ("1") ) 
   { 

      if(Texture) 
      { 
         Debug.Log("Texture Loaded Sucessfully..."); 
         renderer.material.mainTexture = Texture; 
      } 


      else 
      { 
         Debug.Log("Unable to Load texture..."); 
      } 
     } 
    }

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

1 Like

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.

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.

Hello Kei,
try out his one:

public var bildGroesse = 0.1;
var Texture:Texture2D;

function Start () {

    ///////////////GEOMETRIE/////////////////////////////////////
var bildObject : GameObject = new GameObject.CreatePrimitive(PrimitiveType.Plane);
bildObject.transform.position = Vector3(0, 0.5, 0);
bildObject.transform.Rotate(-90,0,0);
bildObject.transform.localScale.x *= bildGroesse;
bildObject.transform.localScale.z *= bildGroesse; //one =(1,1,1)


////////////////////////TEXTURE////////////////////
if(Texture)
      {
         Debug.Log("Texture Loaded Sucessfully...");
         bildObject.renderer.material.mainTexture = Texture;
      }

        else
      {
         Debug.Log("Unable to Load texture...");
      }



}

rombowich, Thanks for the help. Using the game object made it work for me. Thanks.

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...");
}

}
}

351431--12207--$unitycubetexture_270.jpg

Look at this:
http://unity3d.com/support/documentation/ScriptReference/Resources.html

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

rombowich thanks, really helped my day!!!

you are welcome ;-)

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

You can show several separate GUI screens using a "state" integer:-

var state: int;

function OnGUI() {
    if (state == 0) {
      GUI.DrawTexture(texRect, texture1);
      GUI.Label(labelRect, "Login");
    } else if (state == 1) {
      GUI.DrawTexture(texRect, texture2);
      GUI.Label(labelRect, "Details");
    } // etc...
}

You just need to change the value of the state variable depending on what the user does on each screen.