Search Unity

Need help with toggle group

Discussion in 'Scripting' started by Choppa, Feb 11, 2016.

  1. Choppa

    Choppa

    Joined:
    Jan 17, 2015
    Posts:
    5
    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):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Color1Test : MonoBehaviour {
    5.  
    6.     //public Toggle color1;
    7.     //public Toggle color2;
    8.     //public Toggle color3;
    9.     public Renderer rend;
    10.     public Texture[] textures;
    11.  
    12.     void Start()
    13.     {
    14.         rend = GetComponent<Renderer>();
    15.         rend.material.mainTexture = textures[PlayerPrefs.GetInt("SaveColor1", 0)];
    16.     }
    17.  
    18.     public void Red()
    19.     {
    20.         rend.material.mainTexture = textures[0];
    21.         PlayerPrefs.SetInt("SaveColor1", 0);
    22.     }
    23.  
    24.     public void Blue()
    25.     {
    26.         rend.material.mainTexture = textures[1];
    27.         PlayerPrefs.SetInt("SaveColor1", 1);
    28.     }
    29.  
    30.     public void Green()
    31.     {
    32.         rend.material.mainTexture = textures[2];
    33.         PlayerPrefs.SetInt("SaveColor1", 2);
    34.     }
    35. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you could use a switch statement in the start to use the retrieved color value to enable the relevant toggle.
     
  3. Choppa

    Choppa

    Joined:
    Jan 17, 2015
    Posts:
    5
    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):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Color1Test : MonoBehaviour {
    5.  
    6.     public Toggle color1;
    7.     public Toggle color2;
    8.     public Toggle color3;
    9.     public Renderer rend;
    10.     public int currentTexture;
    11.     public Texture [] textures;
    12.     public string savecolorKey = "";
    13.  
    14.     void Start()
    15.     {
    16.         rend = GetComponent<Renderer>();
    17.         //rend.material.mainTexture = textures[PlayerPrefs.GetInt(savecolorKey, currentTexture)];
    18.         currentTexture = PlayerPrefs.GetInt(savecolorKey, currentTexture);
    19.  
    20.         switch (currentTexture)
    21.         {
    22.             case 0:
    23.                 color1.isOn = true;
    24.                 break;
    25.             case 1:
    26.                 color2.isOn = true;
    27.                 break;
    28.             case 2:
    29.                 color3.isOn = true;
    30.                 break;
    31.         }
    32.     }
    33.  
    34.     public void Red()
    35.     {
    36.         currentTexture = 0;
    37.         rend.material.mainTexture = textures[currentTexture];
    38.         PlayerPrefs.SetInt(savecolorKey, currentTexture);
    39.     }
    40.  
    41.     public void Blue()
    42.     {
    43.         currentTexture = 1;
    44.         rend.material.mainTexture = textures[currentTexture];
    45.         PlayerPrefs.SetInt(savecolorKey, currentTexture);
    46.     }
    47.  
    48.     public void Green()
    49.     {
    50.         currentTexture = 2;
    51.         rend.material.mainTexture = textures[currentTexture];
    52.         PlayerPrefs.SetInt(savecolorKey, currentTexture);
    53.     }
    54. }
    And to retrieve color in play scene

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SpawnColorTexture : MonoBehaviour {
    4.  
    5.     public Renderer rend;
    6.     public Texture[] textures;
    7.     public string savecolorKey = "";
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         rend = GetComponent<Renderer>();
    13.         rend.material.mainTexture = textures[PlayerPrefs.GetInt(savecolorKey)];
    14.     }
    15. }