- Home /
 
 
               Question by 
               fyrismyt · Jul 22, 2019 at 09:40 AM · 
                coroutinewaitforseconds  
              
 
              Coroutines WaitForSeconds() won't work,WaitForSeconds() isn't doing anything
I've been trying to make my code work, I'm trying to make it so I can break a tile once every second or so but it still lets me break indefinitely. Help :'(
``` using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; public class DestroyScript : MonoBehaviour {
 [SerializeField]
 private Tilemap ground;
 [SerializeField]
 private Tilemap background;
 public int layer = 0;
 private TileBase tile;
 public TileBase toPlace;
 public TileBase toPlace2;
 public Grid grid;
 public bool CanBreak;
 // Start is called before the first frame update
 void Start()
 {
     StartCoroutine(BreakCoroutine(0.2f));
 }
 // Update is called once per frame
 void Update()
 {
     
     if (Input.GetMouseButton(0))
     {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Vector3Int cellPos = ground.WorldToCell(new Vector3(mousePos.x, mousePos.y, 0));
         ground.RefreshAllTiles();
         if (layer == 0)
         {
             if (!ground.HasTile(cellPos))
             {
                 ground.SetTile(cellPos, toPlace);
             }
         }
         else if (layer == 1)
         {
             if (!background.HasTile(cellPos))
             {
                 background.SetTile(cellPos, toPlace2);
             }
         }
     }
     if (Input.GetMouseButton(1))
     {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Vector3Int cellPos = ground.WorldToCell(new Vector3(mousePos.x, mousePos.y, 0));
         if (layer == 0)
         {
             if (ground.HasTile(cellPos))
             {
                 ground.SetTile(cellPos, null);
             }
         } else if (layer == 1)
         {
             if (background.HasTile(cellPos))
             {
                 background.SetTile(cellPos, null);
             }
         }
     }
     if (Input.mouseScrollDelta.y != 0)
     {
         if (layer == 1)
         {
             layer = 0;
         }
         else if (layer == 0)
         {
             layer = 1;
         }
     }
 }
 void BreakBlock()
 {
 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     Vector3Int cellPos = ground.WorldToCell(new Vector3(mousePos.x, mousePos.y, 0));
     CanBreak = true;
     if (layer == 0)
         {
         if (ground.HasTile(cellPos))
         {
             if (CanBreak)
             {
                 ground.SetTile(cellPos, null);
                 CanBreak = false;
             }
             
         }
     }
     else if (layer == 1)
     {
         if (background.HasTile(cellPos))
         {
             if (CanBreak)
             {
                 background.SetTile(cellPos, null);
                 CanBreak = false;
             }
         }
     }
 }
 IEnumerator BreakCoroutine(float time)
 {
     while(true)
     {
         yield return new WaitForSecondsRealtime(time: 1f);
         Debug.Log("Waited");
         if (Input.GetMouseButtonDown(1))
         {
             BreakBlock();
         }
     }
     
 }
 
               } ```
               Comment
              
 
               
              Your answer