Simple texture change script

Hey, very new to unity and coding but trying my best. I’ve just written a code with the intention of being able to change the texture of a gameobject when the user pushes a button. Here is my code :

var DawnDusk_front : UnityEngine.Texture;



function Update () {

    if (Input.GetButtonDown ("Jump"))

        renderer.material.mainTexture = DawnDusk_front;



}

I’ve just used some example skybox textures I think. The original texture which shows before I press spacebar (kept the “jump” default) is called DawnDusk_back. The issue is, when I press spacebar, instead of the gameobject changing to the correct texture (DawnDusk_front) it just turns a blank grey.

Cheers for your time

I might be wrong, but i think you have the idea that you have to put the name of the texture in the code. Thats not neccesary.

Did u drag the texture from the Hierarchy into the Dawn Dusk front slot in the inspector? Cause here the code seems to work fine. Doesnt matter how you call this though “var DawnDusk_front : UnityEngine.Texture;”

The name of the var doesnt matter as long as the texture is added to the script in the Inspector.

It could be

var textureToBeChanged : UnityEngine.Texture;



function Update () {

    if (Input.GetButtonDown ("Jump"))

        renderer.material.mainTexture = textureToBeChanged;



}

instead. This way the script can also be reused for other textures.

Ahhh gotcha, fantastic. You were right, I thought you had to sort of ‘reference’ the texture to access it. Sorted it now, it’s working fine :slight_smile: Cheers.

It does however lead me to another question though. What if there were a variety of textures that I wanted a gameobject to be able to change to when pressing different buttons. Is it a case of creating several scripts and dragging the textures into the appropriate scripts in the inspector? Or does needing to access multiple scripts affect performance?
Thanks again, really appreciated!

http://unity3d.com/support/documentation/ScriptReference/Array.html

Glad to help :slight_smile:

About using more textures. What Kelso said, put them in an array and access them there depending on which button u press.

Jooly good, jolly good :smile: I’ll get reading my book and googling away then to sort that. Really appreciated lads :slight_smile:

Good luck with that! :slight_smile:

Sounds like you’ve got it, but i’d like to mention its much more optimal to swap materials.

var materialToBeChanged : UnityEngine.Material;
function Update () {
    if (Input.GetButtonDown ("Jump"))
        renderer.sharedMaterial = materialToBeChanged;
}

Reason being the first time you access .material it actually makes a copy of the original material binds it in then returns it to you. .sharedMaterial does not make any copies, but if you modify any values off of .sharedMaterial it actually will change the asset’s value ( like if you changed mainTexture the material in your assets folder actually will be set to use the changed value ).

It might not be a big issue for your project if only one thing is using this script creating a copied material and not using the previous one, though if you for example have this script on a bunch of things, even if the texture its setting to is the same it will make seperate materials each time, making dynamic batching pointless and implies a big overhead on your project. You’ll also be better on memory swapping .sharedMaterial instead of modifying mainTexture.

Ahh right cheers. I only half understood that but I’ll check it again when I think I understand a bit more. I think that your suggestion will be much better seeing as I’m experimenting with this code so I can make a project where I can highlight and select lots of different objects in the game, so performance will definitely be an issue. Cheers for the information

Is there a sharedtexture function as well then? Or does this only apply to materials?