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 :slight_smile:

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;
            }
        }
	}
}

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;
}