Search Unity

Tilemap Variable Not Working

Discussion in '2D' started by Ooka2, Oct 25, 2021.

  1. Ooka2

    Ooka2

    Joined:
    Oct 22, 2020
    Posts:
    3
    ive been trying to figure this one issue out for a while and im stuck - im making a 2d splatoon project. my plan is to have the bullet collide with an uninked tilemap, and then spawn an inked tile on a separate tilemap (so that tilemap can have ink properties). i can access the collision tilemap in the script via OnTriggerEnter2D, but i can't access the inked tilemap within my script. Im trying to assign the tilemap in the editor using public Tilemap inkTileMap but it wont let me drag and drop.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using UnityEditor.Tilemaps;
    6.  
    7.  
    8. // THIS SCRIPT CONTROLS THE INK BULLET
    9.  
    10. public class Bullet : MonoBehaviour
    11. {
    12.  
    13.     public float speed;
    14.     public Rigidbody2D rb;
    15.     public GameObject inkPrefab;
    16.     Tilemap tileMap;
    17.     public Tilemap inkTileMap;
    18.  
    19.  
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         rb.velocity = transform.right * speed; // makes the bullet travel forward
    25.  
    26.     }
    27.  
    28.  
    29.     void OnTriggerEnter2D(Collider2D col)
    30.     {
    31.  
    32.         if(col.gameObject.tag == "UnInked")
    33.         {
    34.             // variable that holds tilemap
    35.             tileMap = col.gameObject.GetComponent<Tilemap>();
    36.  
    37.             Debug.Log(tileMap);
    38.  
    39.             // bulletVelocity takes the bullet's velocity and changes it to either 1 or -1
    40.             // intBulletVelocity takes bulletVelocity and converts it to a Vector3Int (for tiles)
    41.             Vector3 bulletVelocity;
    42.  
    43.             bulletVelocity = rb.velocity;
    44.             bulletVelocity.x = bulletVelocity.x / Mathf.Abs(bulletVelocity.x);
    45.             bulletVelocity.y = bulletVelocity.y / Mathf.Abs(bulletVelocity.y);
    46.             Vector3Int intBulletVelocity = tileMap.WorldToCell(bulletVelocity);
    47.             Debug.Log(bulletVelocity);
    48.  
    49.             // destroys the tile the bullet lands on
    50.             // a bug occurs in this code: the bullet collides with the floor, but the position of the bullet is at the center
    51.             // this means the tile that is replaced is the tile where the bullet's center is. This tile is above the intended tile
    52.             // this is fixed by removing intBulletVelocity from intPosition, making the code think the bullet's center is 1 tile down
    53.             Vector3 worldPoint = transform.position;
    54.             Vector3Int intPosition = tileMap.WorldToCell(worldPoint);
    55.             intPosition.x = intPosition.x + intBulletVelocity.x;
    56.             intPosition.y = intPosition.y + intBulletVelocity.y;
    57.             tileMap.SetTile(intPosition, null);
    58.  
    59.             // creates blue circle and destroys the bullet
    60.             Instantiate(inkPrefab, transform.position, transform.rotation);
    61.             Destroy(gameObject);
    62.         }
    63.  
    64.     }
    65.  
    66. }

    im very confused why this is happening... can anyone explain?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I would set all that aside, make a scene with your two tilemaps (the base one and the inked one), then write a little tiny script to toggle one of the inked squares in the inked map on and off.

    Once you get that working, go back to the above and track down how the data flow from click to hit to tile-fill is different.
     
  3. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Is your MonoBehaviour part of a PrefabAsset? You can usually drag and drop these references if your MonoBehaviour and the Tilemap are part of the same Scene. However, these scene references do not exist outside of the scene, and should not be added to assets outside of the Scene.
     
    mahamcomet likes this.
  4. Ooka2

    Ooka2

    Joined:
    Oct 22, 2020
    Posts:
    3
    That's a good idea. I'm not sure how to access the other tilemap though... I can access and toggle the base one through OnTriggerEnter2D(), but I don't not how I can access the inked one. How would I do that?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742