• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Arisstephenson · Jun 20, 2018 at 08:23 PM · scripting problemtilemaptile

Issue with destroying tiles

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 t$$anonymous$$s 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 t$$anonymous$$s 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 t$$anonymous$$s 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?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Captain_Pineapple · Jun 21, 2018 at 10:13 AM

Hey there,

so far those are the t$$anonymous$$ngs that are a bit weird/perhaps unintended in your code:

Your Bullet will only be able to destroy a Tile when $$anonymous$$tting it with the 5th bounce.

Your DestructibleTile does NOT contain a line that says somet$$anonymous$$ng like Destroy(gameObject); or gameObject.enabled = false;

So somewhere at the End of your Explode() functions you should add one of these functions to disable the tiles. Choses the one or the other depending on if you want to delete them or keep them for possible future reactivation.

Hope t$$anonymous$$s helps, feel free to ask if anyt$$anonymous$$ng is unclear.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with scriptable tiles PLEASE HELP URGENT!!! 1 Answer

How to Change the position of a tile to the player position? 0 Answers

Equivelant "set" method for "getInstantiatedObject"? 0 Answers

Tilemap2017 Access position & sprite of specific Tile in new Grid System 2dGame 1 Answer

2D Tilemap stretching 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges