I am currently making a script for a bullet gameobject that destroys tiles. The script that I use for the bullet and the tile are both below, as is a recording of what happens when I run it. Why don’t any of the tiles get removed? I don’t get any errors either.
_
Script for the bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class BulletDirectionController : MonoBehaviour {
Tilemap tilemap;
Rigidbody2D rig;
int bounces;
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody2D>();
tilemap = GameObject.Find("Grid/Default").GetComponent<Tilemap>();
}
// Update is called once per frame
void Update () {
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(rig.velocity.y, rig.velocity.x) * Mathf.Rad2Deg)), 10);
}
private void OnCollisionEnter2D(Collision2D collision)
{
bounces += 1;
if (bounces > 5) {
TileBase tile = tilemap.GetTile(tilemap.WorldToCell(collision.transform.position)); //Gets tile landed on
if (tile is DestructableTile) {
((DestructableTile)tile).Explode(2); //Explodes with a power of two
}
Destroy(gameObject); //Destroys bullet
}
}
}
_
Script for the tile:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Tilemaps;
[CreateAssetMenu]
public class DestructableTile : TileBase {
public bool Destructing = false;
public Tile.ColliderType ColliderType = Tile.ColliderType.Sprite;
public Sprite m_Sprite;
GameObject gameObject;
Vector3Int _position;
Tilemap _tilemap;
TileData data;
public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
{
_tilemap = GameObject.Find("Grid/Default").GetComponent<Tilemap>();
gameObject = go;
return base.StartUp(position, tilemap, go);
}
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
data = tileData;
tileData.colliderType = ColliderType;
tileData.sprite = m_Sprite;
base.GetTileData(position, tilemap, ref tileData);
_position = position;
}
public void Explode(int power) {
if (power <= 0 || Destructing) { //Return if explosion has reached 0 power or is already destructing
return;
}
Destructing = true;
List<TileBase> tiles = new List<TileBase>();
for (int x = -1; x <= 1; x += 2) { //Finds tiles left and right of this one
TileBase temp = _tilemap.GetTile(_position + new Vector3Int(x, 0, 0));
if (temp != null) {
tiles.Add(temp);
}
}
for (int y = -1; y <= 1; y += 2) { //Finds tiles up and down from this one
TileBase temp = _tilemap.GetTile(_position + new Vector3Int(0, y, 0));
if (temp != null) {
tiles.Add(temp);
}
}
foreach (TileBase tile in tiles) {
if (tile is DestructableTile) //Determine if tile we found is a DestructableTile
{
DestructableTile dTile = (DestructableTile)tile;
dTile.Explode(power - 1);
}
}
_tilemap.SetTile(_position, null);
_tilemap.RefreshTile(_position);
}
}
_
Here is what happens: https://streamable.com/974ax
Can someone please explain what I am doing wrong?