Changing Texture of Window Asset (2 part asset)

I created a small scene in unity with a menu and two windows. I wrote a script to change the background image of a window but the texture gets assigned to another part of the asset (wooden window frame). The window was added to my project and has two textures, one was wood and the other was glass. I had removed the glass was using a blank gameobject with applied textures to go in its place. I was trying to use my script to call these based on what is selected in the menu. Can anyone look at this code and tell me if I’m going about it wrong?

using UnityEngine;

using System.Collections;

using TMPro;

public class TextureSwitcher : MonoBehaviour

{

public Texture2D texture1; // Assign “Day” texture

public Texture2D texture2; // Assign “Dusk” texture

public TMP_Dropdown Dropdown; // Public reference to TMP_Dropdown

private Renderer[ ] renderers;

void Start()

{

renderers = GetComponentsInChildren();

UpdateAllMaterials();

// Ensure Dropdown is assigned in the Inspector

if (Dropdown == null)

{

Debug.LogError(“Please assign the TMP_Dropdown component in the Inspector!”);

return;

}

Dropdown.onValueChanged.AddListener(OnDropdownValueChanged);

}

void UpdateAllMaterials()

{

Texture textureToSet = texture1;

if (Dropdown.value == 1)

textureToSet = texture2;

for (int i = 0; i < renderers.Length; i++)

renderers*.material.mainTexture = textureToSet;*
}
void OnDropdownValueChanged(int value)
{
// Check for “1” to switch to Dusk texture
UpdateAllMaterials();
}
}

Use code tags.
[ Using code tags properly ]( Using code tags properly [/CODE])

They pasted in code that compiles. The * starts an italicized block, with the tag itself being removed from the rendered text. You’ll notice the text becomes italicized after that point.*
Yet another reason to use code tags.

1 Like

Does your window have multiple materials?
You can access the materials of the window which gives you an array of materials for the renderer.

for (int i = 0; i < renderers.Length; i++) {
            var windowMaterials = renderers [i].materials;
            windowMaterials [1].mainTexture = textureToSet;  //Apply the texture to the 2nd material    
}

If you don’t know the order of the materials, or can’t rely on it, you can analize the name of the material. For instance, if you knew the name of the material contained the text “glass”:

for (int i = 0; i < renderers.Length; i++) {
            var windowMaterials = renderers [i].materials;

            for (int j = 0; j < windowMaterials.Length; j++) {              
                if (windowMaterials [j].name.Contains ("glass")){
                    windowMaterials [j].mainTexture = textureToSet;
                    break;
                }
            }
}

Yes, it has two materials, the wood frame and then an opaque glass game object titled glass so I removed that to add my own. I just can’t script for it to be set by user selection. I will definitely try code tags.

It worked! , thank you to you both for your time. I appreciate it :slight_smile: