So I want to make a merge idle game, but I have a problem loading data from playerprefs, I have an item that I bought from the store so obj counts are increasing but at the same time I want to check another item bought from the store, for example, I buy obj1 twice, and obj2 3 times so obj counts are total of those items 2+3=5.
What I've tried:
- I'm using for loop for that, but I am confused about how to check for another item.
- using a nested loop, still doesn't work.
Conditions have met:
- when I bought from the store the
obj countincreased. - when the object merged
obj countdecreased; - save current object with
playerprefs.set("currItemBought+index,itemBought), - save current current obj index when buy obj.
What I want:
- every time I start the game again I load data from the playerprefs then the object that spawns must be the same from I bought for example when I buy obj2 twice then when re-start, obj2 must spawn twice, or another example when I buy
obj1once andobj23 times so when re-start must spawnobj1once andobj23 times so totalobj countis 4.
Preview:
My Script:
Load Data
//load Data
public void LoadData()
{
if (PlayerPrefs.HasKey("objCount"))
{
// get tier to save
objCount = PlayerPrefs.GetInt("objCount");
currHighesTier = PlayerPrefs.GetInt("tier");
Debug.Log(objCount);
Debug.Log("tier: " + currHighesTier);
for (int i = 0; i < objCount; i++)
{
if (currHighesTier == i)
{
SpawnObject(currHighesTier);
}
else
{
SpawnObject(0);
}
}
}
else
{
objCount = 1;
PlayerPrefs.SetInt("objCount", objCount);
SpawnObject(0);
}
}
Buy Item
public void Buy_Object(int index)
{
currCharacter = UI_Manager.instance.characterDB.GetCharacter(index);
for (int i = 0; i < UI_Manager.instance.shopItemsContainer.childCount; i++)
{
if (index == i)
{
if (Coin >= characters.characters[index].basePrice * (UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought + 1))
{
objCount++;
PlayerPrefs.SetInt("objCount", objCount);
Add_Coin(-characters.characters[index].basePrice * (UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought + 1));
UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought++;
PlayerPrefs.SetInt("currItemBough" + index, UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought);
SpawnObject(index);
}
else
{
Debug.Log("Not enough coin");
}
}
}
}
Evolve when merging
private void Evolve()
{
//Set object count
GameManager.instance.objCount--;
PlayerPrefs.SetInt("objCount", GameManager.instance.objCount);
UI_Manager.instance.shopItemsContainer.GetChild(tier).GetComponent<CharacterItemUI>().itemBought--;
PlayerPrefs.SetInt("currItemBough" + tier, UI_Manager.instance.shopItemsContainer.GetChild(tier).GetComponent<CharacterItemUI>().itemBought);
evolve_particle.Play();
LevelProgressUI.Instance.AddXP((tier + 1) * 25);
tier++;
gameObject.name = "Obj" + tier;
GameManager.instance.CheckTier(tier);
SetObject();
}
Check Tier
public void CheckTier(int tier)
{
if (tier > highestTier)
{
highestTier = tier;
PlayerPrefs.SetInt("tier", highestTier);
StartCoroutine(UI_Manager.instance.NewTier());
characters.characters[tier].isUnlocked = true;
UI_Manager.instance.UpdateUI(tier);
}
else
{
PlayerPrefs.SetInt("tier", tier);
}
}
`Sorry for the preview I use youtube due to 1-minute videos. from the video when I restart the game the obj that was bought twice only spawned once.