- Home /
Toggle Textures Script
I've been asking for help on creating a script that toggles all textures in the scene on and off. I'm currently using this script which works to an extent. It toggles textures off and on however upon toggling back on the textures are default textures. I require the old Texture to be able to come back for this script to be considered fully functional. I'll be working on this for a while and post back an answer If I find a solution. Untill then I'd appreciate any ideas on how to achieve this. Thanks :)
using UnityEngine;
using System.Collections;
public class TexturesScript : MonoBehaviour {
public Texture texture;
public bool isOn = true;
void Start ()
{
isOn = !isOn;
Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
foreach (Renderer renderer in renderers)
{
renderer.sharedMaterial.mainTexture = isOn ? texture : null;
}
}
void Update ()
{
if(Input.GetButtonUp("Jump"))
{
isOn = !isOn;
Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
foreach (Renderer renderer in renderers)
{
renderer.sharedMaterial.mainTexture = isOn ? texture : null;
}
}
}
}
Answer by robhuhn · Sep 09, 2013 at 11:51 AM
Store the textures on start and map them to the renderer instead of passing it through the editor (code not testet):
using System.Collection.Generic;
...
private Dictionary<Renderer, Texture> textureForRenderer = new Dictionary<Renderer, Texture>();
void Start ()
{
isOn = !isOn;
Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer));
foreach (Renderer renderer in renderers)
{
textureForRenderer[renderer] = renderer.sharedMaterial.mainTexture
renderer.sharedMaterial.mainTexture = isOn ? texture : null;
}
}
Get the desired texture using the renderer object:
foreach (Renderer renderer in renderers)
{
renderer.sharedMaterial.mainTexture = isOn ? textureForRenderer[renderer] : null;
}
I've tested this script out and I'm getting the error "The given key was not present in the dictionary"
any idea what could be going wrong? :(
I think I've found an issue, If there are 2 objects in the scene using the same renderer
The texture will be saved the first time and the current texture will be set to null.
The next time the foreach loop finds the same object the current texture will be null and it will overwrite the dictionary
Done some more testing and another problem seems to be reading textures from the dictionary. I'm not sure if the syntax is incorrect of if there is something wrong with the code but
renderer.shared$$anonymous$$aterial.mainTexture = isOn ? textureForRenderer[renderer] : null;
Produces errors
The syntax should be ok. First of all I would declare Renderer[] renderers as a private field variable, there is no need to fetch it every time I guess.
Defining the Renderer/Texure dictionary is fine too but a $$anonymous$$or improvement would be to change this to $$anonymous$$aterial/Texture:
private Dictionary<$$anonymous$$aterial, Texture> textureFor$$anonymous$$aterial = new Dictionary<$$anonymous$$aterial, Texture>();
textureFor$$anonymous$$aterial[renderer.shared$$anonymous$$aterial] = renderer.shared$$anonymous$$aterial.mainTexture;
If there are 2 objects in the scene using the same renderer
Don't know how it would be possible that two objects use the same renderer instance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113835im_/https://answers.unity.com/themes/thub/images/avi.jpg)