Question by
kuivalappa · Mar 12, 2017 at 06:28 PM ·
c#gameobjectplayerprefsindexshop
Please help how to save last index and load
Hi everyone
can someone smart show me how to save last activated gun or last index got activated in playerprefs i m pretty new in coding and stupid, so please don't tell how to do it, show me :D thanks for help and sorry for my bad english ..
public class TestGun : MonoBehaviour {
public GameObject[] gun;
public int activeIndex = 0;
public void SetActiveObject(int aIndex)
{
activeIndex = aIndex;
for (int i = 0; i < gun.Length; i++)
gun[i].SetActive(i == activeIndex);
}
void Update()
{
SetActiveObject(activeIndex);
}
public void UpgradeButtom()
{
activeIndex++;
}
}
Comment
Best Answer
Answer by kuivalappa · Mar 13, 2017 at 12:36 AM
public class testest : MonoBehaviour {
public GameObject[] gun;
public int activeIndex = 0;
void Start()
{
activeIndex = PlayerPrefs.GetInt("guns");
}
public void SetActiveObject(int aIndex)
{
activeIndex = aIndex;
for (int i = 0; i < gun.Length; i++)
gun[i].SetActive(i == activeIndex);
PlayerPrefs.SetInt("guns", activeIndex);
}
void Update()
{
SetActiveObject(activeIndex);
}
public void UpgradeButtom()
{
activeIndex++;
}
}