IEnumerator SaveNow() { waveManager = GameObject.FindGameObjectWithTag("WaveManager").GetComponent <WaveManager>(); data.Wave = waveManager.GetWave(); yield return(new WaitForSeconds(0.1f)); resourceSystem = GameObject.FindGameObjectWithTag("ResourceManager").GetComponent <ResourceSystem>(); data.plutonium = resourceSystem.GetPlutonium(); data.ore = resourceSystem.GetOre(); GameObject[] listTower = GameObject.FindGameObjectsWithTag("Tower"); foreach (GameObject obj in listTower) { if (obj != null) { Towers temp = new Towers(); temp.postion = obj.transform.position; temp.health = obj.GetComponent <Tower>().currentHP; data.AttackTowers.ListsOfTowers.Add(temp); } } listTower = GameObject.FindGameObjectsWithTag("Energy"); foreach (GameObject obj in listTower) { if (obj != null) { Towers temp = new Towers(); temp.postion = obj.transform.position; temp.health = obj.GetComponent <Tower>().currentHP; data.EnergyTowers.ListsOfTowers.Add(temp); } } listTower = GameObject.FindGameObjectsWithTag("Resource"); foreach (GameObject obj in listTower) { if (obj != null) { Towers temp = new Towers(); temp.postion = obj.transform.position; temp.health = obj.GetComponent <Tower>().currentHP; data.MiningTowers.ListsOfTowers.Add(temp); } } listTower = GameObject.FindGameObjectsWithTag("Wall"); foreach (GameObject obj in listTower) { if (obj != null) { Towers temp = new Towers(); temp.postion = obj.transform.position; temp.health = obj.GetComponent <Tower>().currentHP; data.WallTower.ListsOfTowers.Add(temp); } } s_Data = JsonUtility.ToJson(data); Debug.Log(s_Data); PlayerPrefs.SetString("SaveSlot", s_Data); }