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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; using UnityEditor.Tilemaps; // THIS SCRIPT CONTROLS THE INK BULLET public class Bullet : MonoBehaviour { public float speed; public Rigidbody2D rb; public GameObject inkPrefab; Tilemap tileMap; public Tilemap inkTileMap; // Start is called before the first frame update void Start() { rb.velocity = transform.right * speed; // makes the bullet travel forward } void OnTriggerEnter2D(Collider2D col) { if(col.gameObject.tag == "UnInked") { // variable that holds tilemap tileMap = col.gameObject.GetComponent<Tilemap>(); Debug.Log(tileMap); // bulletVelocity takes the bullet's velocity and changes it to either 1 or -1 // intBulletVelocity takes bulletVelocity and converts it to a Vector3Int (for tiles) Vector3 bulletVelocity; bulletVelocity = rb.velocity; bulletVelocity.x = bulletVelocity.x / Mathf.Abs(bulletVelocity.x); bulletVelocity.y = bulletVelocity.y / Mathf.Abs(bulletVelocity.y); Vector3Int intBulletVelocity = tileMap.WorldToCell(bulletVelocity); Debug.Log(bulletVelocity); // destroys the tile the bullet lands on // a bug occurs in this code: the bullet collides with the floor, but the position of the bullet is at the center // this means the tile that is replaced is the tile where the bullet's center is. This tile is above the intended tile // this is fixed by removing intBulletVelocity from intPosition, making the code think the bullet's center is 1 tile down Vector3 worldPoint = transform.position; Vector3Int intPosition = tileMap.WorldToCell(worldPoint); intPosition.x = intPosition.x + intBulletVelocity.x; intPosition.y = intPosition.y + intBulletVelocity.y; tileMap.SetTile(intPosition, null); // creates blue circle and destroys the bullet Instantiate(inkPrefab, transform.position, transform.rotation); Destroy(gameObject); } } } im very confused why this is happening... can anyone explain?
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.
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.
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?
Same way you access EVERYTHING in Unity: Referencing variables, fields, methods (anything non-static) in other script instances: https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639 https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/