Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Select a list of tiles through script

Discussion in '2D' started by Buck595, Apr 8, 2020.

  1. Buck595

    Buck595

    Joined:
    Mar 24, 2019
    Posts:
    4
    Hello!
    I am currently making a game in which tiles are destroyable. I am rather new to Unity, and don't really know how to achieve this, maybe you could help me! :)

    I am looking for a way to select all tiles inside a defined rectangular area through script, so I could remove them (set them to null) if there is one.
    I already have the setup to create and remove tiles, but I can't find the way to select multiple tiles within chosen boundaries.

    Any way I could do that? I hope I'm clear in my request.
    Thanks!
     
  2. Entapsia

    Entapsia

    Joined:
    Aug 2, 2016
    Posts:
    3
    Hi,

    You should be able to do it with the detection of mouseclick.

    When the mouse button is pressed, get the position of the object under it, and when it is released, do the same.

    Then, destroy any object with positions beetween these two vectors.
     
    Buck595 likes this.
  3. Buck595

    Buck595

    Joined:
    Mar 24, 2019
    Posts:
    4
    Thanks for the reply, but I'd like to do it through script, as it will be always the same known area. Do you know how I could get a list of positions ?
     
  4. Entapsia

    Entapsia

    Joined:
    Aug 2, 2016
    Posts:
    3
    I don't know if i get it.

    If the area is always the same, you can use a Box Collider 2D for that area and make it trigger, change the tag of any tile to the new tag "Tile" and then attach a script like this to your area :

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Area : MonoBehaviour
    6. {
    7.     private readonly List<GameObject> tilesColliding = new List<GameObject>();
    8.  
    9.     public void OnTriggerEnter2D(Collider2D col)
    10.     {
    11.         //Check if it's a tile and not already in the list
    12.         if (col.gameObject.CompareTag("tile") && !this.tilesColliding.Contains(col.gameObject))
    13.             this.tilesColliding.Add(col.gameObject);
    14.     }
    15.     public void OnTriggerExit2D(Collider2D col)
    16.     {
    17.         //Check if it's a tile ands still in the list
    18.         if (col.gameObject.CompareTag("tile") && this.tilesColliding.Contains(col.gameObject))
    19.             this.tilesColliding.Remove(col.gameObject);
    20.     }
    21.  
    22.     //And when you want to get positions of colliding tiles, you do this :
    23.     public List<Vector3> GetTilesPositions()
    24.     {
    25.         GameObject[] tiles = GameObject.FindGameObjectsWithTag("Tile");
    26.         List<Vector3> positions = new List<Vector3>();
    27.  
    28.         foreach (GameObject tile in tiles)
    29.         {
    30.             if (this.tilesColliding.Contains(tile))
    31.             {
    32.                 positions.Add(tile.transform.position);
    33.             }
    34.         }
    35.  
    36.         return positions;
    37.     }
    38. }
    39.  
    Don't forget to add a RigidBody2D to your area and colliders to your tiles.
    For tiles, make colliders small, and at the center of the tile if you don't want a tile to be considered as selected when its edge is colliding with the area.
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Buck595

    "looking for a way to select all tiles inside a defined rectangular area through script"
    "so I could remove them (set them to null) if there is one."


    Simply use 2 nested for loops, nothing else needed.

    You could use bounds class to define min and max corners of this area, and then use bounds min and max values in your for loop.

    Then iterate y-axis, inside this loop, iterate x-axis and set tiles to null.

    Edit - and there's also this:
    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetTilesBlock.html