So i was trying to use PlayerPrefs to save play progess when quit the game
code use good in all mode but in Game Mode 3 it was auto duplicate my prefabs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GameManager : MonoBehaviour
{
public int _gameMode;
public List<GameObject> prefabs;
int index;
public List<int> num;
public List<int> _newNum;
int rnd;
public float _power;
public GameObject particleSys;
public static GameManager instance;
public TextMeshProUGUI txtNew;
public GameObject _new;
float timeDelayMerge = 0.01f;
public bool canMerge = true;
float timeTemp;
float timeTempNew;
float timeNewDelay = 2f;
Vector2 newTarget;
GameObject _2048;
public List<GameObject> _droppedList;
public string _name;
int _combo = 0;
public float timeTempCombo;
public float timeDelayCombo = 0.1f;
GameObject _explode;
private void Awake()
{
instance = this;
}
private void Start()
{
LoadNewBox();
_gameMode = PlayerPrefs.GetInt("SellectedMode");
if (PlayerPrefs.GetInt("Played") == 1)
{
if (_gameMode == 3)
{
for (int i = 0; i < PlayerPrefs.GetInt("listCount"); i++)
{
for (int j = 0; j < 3; j++)
{
_name = i + " " + j;
LoadGame(prefabs[j], _name);
}
}
}
else for (int i = 0; i < PlayerPrefs.GetInt("listCount"); i++)
{
_name = i + " " + _gameMode;
LoadGame(prefabs[_gameMode], _name);
}
}
}
private void Update()
{
//Test Zone
//EndTest
UIManager.instance.Pause();
if (Time.time - timeTemp > timeDelayMerge)
{
canMerge = true;
}
if (Time.time - timeTempNew > timeNewDelay)
{
_new.SetActive(false);
txtNew.text = "";
}
if (Time.time - timeTempCombo > timeDelayCombo)
{
_combo = 0;
}
}
public int droppedCount = 0;
public void merge(Transform posSpawn, int n, GameObject Box, GameObject detroyedBox, float impactForce)
{
if (canMerge)
{
if (GameManager.instance._droppedList.Contains(detroyedBox)) GameManager.instance._droppedList.Remove(detroyedBox);
if (GameManager.instance._droppedList.Contains(Box)) GameManager.instance._droppedList.Remove(Box);
Vector2 direction = Box.transform.position - detroyedBox.transform.position;
_2048 = Instantiate(TouchManager.instance.objectToSpawn, Box.transform.position, Quaternion.identity);
_2048.name = TouchManager.instance.objectToSpawn.name;
_droppedList.Add(_2048);
Destroy(detroyedBox);
Destroy(Box);
n *= 2;
if (!num.Contains(n))
{
txtNew.text = "New Box: " + n;
_new.SetActive(true);
if (index > 10) index = 10;
SoundManager.instance.PlaySound(3);
timeTempNew = Time.time;
_newNum.Add(n);
num.Add(n);
}
SoundManager.instance.PlaySound(2);
timeTempCombo = Time.time;
_combo++;
if (_combo == 2)
{
timeTempCombo = Time.time;
}
else if (_combo == 3)
{
timeTempCombo = Time.time;
}
else if (_combo == 4)
{
timeTempCombo = Time.time;
}
else if (_combo >= 5)
{
timeTempCombo = Time.time;
}
_2048.transform.GetChild(1).gameObject.SetActive(false);
_explode = Instantiate(particleSys, new Vector3(_2048.transform.position.x, _2048.transform.position.y, _2048.transform.position.z - 1), Quaternion.identity);
for (int i = 0; i < num.Count(); i++)
if (n == num[i]) index = i;
_2048.GetComponent<NumberGen>().Init(n, index);
_2048.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
_2048.tag = "Dropped";
_2048.GetComponent<Rigidbody2D>().AddForce(direction * impactForce * _power, ForceMode2D.Impulse);
UIManager.instance.addScore(n);
timeTemp = Time.time;
canMerge = false;
}
}
public void GameOver()
{
DeleteKey();
DeleteNewBox();
UIManager.instance.LoserPopUp();
UIManager.instance._pausing = true;
}
public int random()
{
rnd = Random.Range(0, num.Count() - 1);
return rnd;
}
public int random2048()
{
rnd = Random.Range(0, 3);
return rnd;
}
public bool RandomMode(int index)
{
if (index == 3)
{
return true;
}
else return false;
}
public void SaveNewBox()
{
PlayerPrefs.SetInt("Arr.Len", _newNum.Count);
for (int i = 0; i < PlayerPrefs.GetInt("Arr.Len"); i++)
{
PlayerPrefs.SetInt("num" + i, _newNum[i]);
}
}
public void LoadNewBox()
{
if (PlayerPrefs.GetInt("Arr.Len") != 0)
{
for (int i = 0; i < PlayerPrefs.GetInt("Arr.Len"); i++)
{
if (PlayerPrefs.GetInt("num" + i) != 0)
{
num.Add(PlayerPrefs.GetInt("num" + i));
_newNum.Add(PlayerPrefs.GetInt("num" + i));
}
}
}
}
public void SaveGameMix(GameObject _dropped, string _name)
{
}
public void SaveGame(GameObject _dropped, string _name)
{
PlayerPrefs.SetInt("listCount", _droppedList.Count);
PlayerPrefs.SetInt("gameMode", _gameMode);
PlayerPrefs.SetFloat(_name + "X", _dropped.transform.position.x);
PlayerPrefs.SetFloat(_name + "Y", _dropped.transform.position.y);
PlayerPrefs.SetFloat(_name + "Rot", _dropped.transform.eulerAngles.z);
PlayerPrefs.SetInt("Score", UIManager.instance.score);
PlayerPrefs.SetInt(_name + "num", _dropped.GetComponent<NumberGen>().number);
PlayerPrefs.SetInt(_name + "color", _dropped.GetComponent<NumberGen>().colornum);
}
public void LoadGame(GameObject _dropped, string _name)
{
if (PlayerPrefs.GetInt(_name + "num") != 0)
{
UIManager.instance._continue.SetActive(true);
UIManager.instance._pausing = true;
GameObject _saveddropped;
_saveddropped = Instantiate(_dropped, new Vector2(PlayerPrefs.GetFloat(_name + "X"), PlayerPrefs.GetFloat(_name + "Y")), Quaternion.Euler(0, 0, PlayerPrefs.GetFloat(_name + "Rot")));
_saveddropped.name = _dropped.name;
UIManager.instance.score = PlayerPrefs.GetInt("Score");
UIManager.instance.txtScore.text = UIManager.instance.score.ToString();
_saveddropped.tag = "Dropped";
_saveddropped.GetComponent<NumberGen>().Init(PlayerPrefs.GetInt(_name + "num"), PlayerPrefs.GetInt(_name + "color"));
_saveddropped.transform.GetChild(1).gameObject.SetActive(false);
_saveddropped.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
_droppedList.Add(_saveddropped);
}
}
public void DeleteNewBox()
{
for (int i = 0; i < PlayerPrefs.GetInt("Arr.Len", 0); i++)
{
PlayerPrefs.DeleteKey("num" + i);
}
PlayerPrefs.DeleteKey("Arr.Leng");
}
public void DeleteKey()
{
for (int i = 0; i < _droppedList.Count; i++)
{
_name = i + " " + GameManager.instance._gameMode;
PlayerPrefs.DeleteKey(_name + "X");
PlayerPrefs.DeleteKey(_name + "Y");
PlayerPrefs.DeleteKey(_name + "Rot");
PlayerPrefs.DeleteKey("Score");
PlayerPrefs.DeleteKey(_name + "num");
PlayerPrefs.DeleteKey(_name + "color");
}
PlayerPrefs.DeleteKey("listCount");
}
private void OnApplicationQuit()
{
SaveNewBox();
if (_gameMode == 3)
{
for (int i = 0; i < _droppedList.Count; i++)
{
for (int j = 0; j < 3; j++)
{
if (_droppedList[i].name == prefabs[j].name)
{
_name = i + " " + j;
SaveGame(_droppedList[i], _name);
}
}
}
}
else for (int i = 0; i < _droppedList.Count; i++)
{
_name = i + " " + _gameMode;
SaveGame(_droppedList[i], _name);
}
}
}
[ICODE][/ICODE]