- Home /
c# - Change a texture so it stays like that when i change scenes
So I made this script where I would press C and it will swap textures. But can I make it so I can choose a texture from main menu and then use that texture throughout my game (which is a different scene)? Is there a way to make it so the option would be bound to a script until it's changed? heres the script with the texture swap if it helps
public Material[] textures; public int currentTexture;
void Update ()
{
if (Input.GetKeyDown(KeyCode.C))
{
currentTexture++;
currentTexture %= textures.Length;
GetComponent<Renderer>().material.mainTexture = textures[currentTexture];
}
} so can i somehow make it so i choose the texture in main menu and it will stay like that throughout the entire game, even if i change scenes? thanks in advance
Answer by phil_me_up · Feb 21, 2016 at 12:10 PM
You'll probably want to have some instanced manager class 'TextureManager' which persists through scene changes (see DontDestroyOnLoad). This will hold the possible textures and a reference to the texture you want to us.
Now when you load each scene, anything which should have the new texture assigned can look up the correct value from the TextureManager instance (something like TextureManager.GetInstance().GetCurrentTexture()).
Answer by bitstrider · Feb 22, 2016 at 09:52 AM
If you wanna persist data across Scenes, you could try PlayerPrefs. You just need a way to reference the desired texture with a string, int, or float.