Hello there, just started working on my game where i can change cars color and i wanna know how can i save last active toggle in toggle group so when i leave game or change scene selected toggle is still active. Thank you. This is my scrip I use for texture swap Code (CSharp): using UnityEngine; using UnityEngine.UI; public class Color1Test : MonoBehaviour { //public Toggle color1; //public Toggle color2; //public Toggle color3; public Renderer rend; public Texture[] textures; void Start() { rend = GetComponent<Renderer>(); rend.material.mainTexture = textures[PlayerPrefs.GetInt("SaveColor1", 0)]; } public void Red() { rend.material.mainTexture = textures[0]; PlayerPrefs.SetInt("SaveColor1", 0); } public void Blue() { rend.material.mainTexture = textures[1]; PlayerPrefs.SetInt("SaveColor1", 1); } public void Green() { rend.material.mainTexture = textures[2]; PlayerPrefs.SetInt("SaveColor1", 2); } }
you could use a switch statement in the start to use the retrieved color value to enable the relevant toggle.
Thank you so much for reply and pointing me in the right direction This is what i came up with and it works for what i need. Code (CSharp): using UnityEngine; using UnityEngine.UI; public class Color1Test : MonoBehaviour { public Toggle color1; public Toggle color2; public Toggle color3; public Renderer rend; public int currentTexture; public Texture [] textures; public string savecolorKey = ""; void Start() { rend = GetComponent<Renderer>(); //rend.material.mainTexture = textures[PlayerPrefs.GetInt(savecolorKey, currentTexture)]; currentTexture = PlayerPrefs.GetInt(savecolorKey, currentTexture); switch (currentTexture) { case 0: color1.isOn = true; break; case 1: color2.isOn = true; break; case 2: color3.isOn = true; break; } } public void Red() { currentTexture = 0; rend.material.mainTexture = textures[currentTexture]; PlayerPrefs.SetInt(savecolorKey, currentTexture); } public void Blue() { currentTexture = 1; rend.material.mainTexture = textures[currentTexture]; PlayerPrefs.SetInt(savecolorKey, currentTexture); } public void Green() { currentTexture = 2; rend.material.mainTexture = textures[currentTexture]; PlayerPrefs.SetInt(savecolorKey, currentTexture); } } And to retrieve color in play scene Code (CSharp): using UnityEngine; public class SpawnColorTexture : MonoBehaviour { public Renderer rend; public Texture[] textures; public string savecolorKey = ""; // Use this for initialization void Start () { rend = GetComponent<Renderer>(); rend.material.mainTexture = textures[PlayerPrefs.GetInt(savecolorKey)]; } }