using System.Collections; using System.Collections.Generic; using UnityEngine; public class breakable : MonoBehaviour { public string name; public string name2; public Sprite[] sprites; private int oldSprite; private int newSprite; private List availableSprites = new List(); public ParticleSystem collisionParticleSystem; public bool timeToBreak = false; private int isBroken = 0; void Start() { for(int i=0; i<3; i++) { availableSprites.Add(i); } int continued = PlayerPrefs.GetInt("continued"); if (continued == 1) { isBroken = PlayerPrefs.GetInt(name2); } else { isBroken = PlayerPrefs.GetInt(name); } if(isBroken == 0) { oldSprite = 0; GetComponent().sprite = sprites[oldSprite]; } else if (isBroken == 1) { oldSprite = 1; GetComponent().sprite = sprites[oldSprite]; } else if (isBroken == 2) { oldSprite = 2; GetComponent().sprite = sprites[oldSprite]; } else if(isBroken == 3) { gameObject.SetActive(false); } } IEnumerator Timer() { if (timeToBreak == true){ isBroken = 3; PlayerPrefs.SetInt(name, isBroken); gameObject.SetActive(false); } yield return new WaitForSeconds(1); var em = collisionParticleSystem.emission; em.enabled = false; } private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "PickaxeHitbox") { var em = collisionParticleSystem.emission; em.enabled = true; collisionParticleSystem.Play(); if (oldSprite == 0) { newSprite = availableSprites[1]; oldSprite = 1; GetComponent().sprite = sprites[newSprite]; isBroken = 1; } else if (oldSprite == 1) { newSprite = availableSprites[2]; oldSprite = 2; GetComponent().sprite = sprites[newSprite]; isBroken = 2; } else if (oldSprite == 2) { timeToBreak = true; StartCoroutine(Timer()); } GetComponent().sprite = sprites[newSprite]; StartCoroutine(Timer()); } } public void SaveCheckpoint() { PlayerPrefs.SetInt(name2, isBroken); } }