- Home /
Question by
unity_NErgEZ563bxDEg · Nov 15, 2020 at 02:33 AM ·
2dtilemaptileraycasthit2d
How to reference a float from a specific tile?
I've made a laser in 2D that draws a line from the player to the hit collider with Raycast2D and shaders. I then got the tilemap from hit and the specific grid coords with this script:
Tilemap mineable = hit.collider.GetComponent<Tilemap>();
Vector3 hitPosition = hit.point;
hitPosition.x = hit.point.x;
hitPosition.y = hit.point.y;
Vector3Int gridCell = mineable.WorldToCell(hitPosition);
However I want to make it so that the laser takes time to destroy the tiles, so I added a default gameobject (prefab) with a float for durability to the ruletile that I use to draw the mineable tiles. The problem is, how do I reference the specific hit tile in such a way that I can change the durability value from the hit tilemap "mineable"?
Comment
Best Answer
Answer by unity_NErgEZ563bxDEg · Nov 15, 2020 at 03:13 PM
So it turns out I can reference the instantiated object like this:
GameObject tileAtPosition = mineable.GetInstantiatedObject(gridCell);
float durability = tileAtPosition.GetComponent<OresScript>().Durability;
the first line gets the game object, and the second line gets the value.