I am using the new tilemap feature in unity 2017, how do I store information in a specific tile?
In my game a character can chop down trees that are represented by a tree tile that is an extension of Tilebase. I need the tree tile to have an int that refers to the number of trees left. I have a Sprite array 0-7 that shows the 8 levels of trees are present then it removes the tile after it goes below 0; For some reason the int numtrees is shared across all tree tiles in my game and is not unique to the tile. How do I give my tiles a unique numtrees and change it in game place so that the sprite changes and eventually the tile is removed?
I can remove the tile just fine using setTile method but I can not change an individual tile's numtrees and change its sprite in runtime.
Answer by Dried09 · Jan 09, 2018 at 08:34 AM
Hi! Storing data of individual tiles is really problem, I'm stucked with it too. Whole last week I trying to find normal solution, asking people on forum\answers\stackoverflow and found out something.
First, tiles are scriptable objects, they not inherit MonoBehaviour and, roughly speaking, has a static data inside by default, because it is normal behaviour for scriptable objects in Unity. Tiles data storing in a struct TileData, which exists for each tile in a Tilemap GameObject. TileData has a "Instanced GameObject" field which may to contain regular Unity GameObject with any logic in its components what you need, Unity creates one for all tiles and will attach to Tilemap as children. BUT, I didn't find more or less normal way to get/set data in this GameObject, it's very uncomfortable to access in different ways tiles (Set/GetTiles()) and regular GameObjects. I also tried to access this GOs via Tilemap, but in this way is possible to access only scriptable object (which is "static" and share values to all).
Second, I found additional extensions for default TileMap features in Unity by, as I understand, TileMap developers. Among others it contains a GridInformation script, which looks like a some compromise solution of tiles data storing. It has no any docs\references\examples, but it looks simple. You can find it here. As I understand GridInformation script allows to get\set properties (like dictionary, key/value) by grid positions. I didn't dig deep, but as for me it's not a good solution, so many additional code to do so simple things.
I will continue to find optimal solution and will be grateful for help too.
Answer by ryandotdee · Dec 24, 2017 at 01:21 AM
I am looking for the same information. I have tried attaching a gameobject to each tile at runtime and setting / storing values on it. But I run into the same problem. I am starting to think that it it not possible, in which case I will need to make an array of objects which are storing the "state" of the tile, referenced by its cell position.
But if I am wrong I would be very keen to know the answer!
Answer by mbtthew · Dec 05, 2017 at 04:33 AM
Did you ever find a solution? I’m having trouble with this one as well. ,Where you ever able to solve this? I’m have the same query and can’t find a solution.
Answer by unity_ZXTycjh-ddLLDg · Dec 25, 2017 at 08:23 PM
Im rather new so sorry if this response is ignorant. And my apologizes but i did not use tileMap when using this script but plan too so would be glad to know it will work. however it does relate to storing individual stats in a gameObject, in this case one that is instantiated repeatably but each instantiate (clone) carries individual stats derived from the same script attached to the prefab (in OP case the Tree).
an array is used to represent each state of the tree:
[SerializeField]
private Transform[] difStatesOfTree;
private int pointsToNextState;
At Start (may not be needed, probably depends on case of use)
difStatesOfTree[0].gameObject.SetActive(true);
in the Update function (im aware there are more efficient ways of doing this, just not savvy on the use)
if (pointsToNextState >= 1)
{
difStatesOfTree[0].gameObject.SetActive(false);
difStatesOfTree[1].gameObject.SetActive(true);
difStatesOfTree[2].gameObject.SetActive(false);
difStatesOfTree[3].gameObject.SetActive(false);
Debug.Log("difStatesOfTree Level 1");
}
if (pointsToNextState >= 5)
{
difStatesOfTree[0].gameObject.SetActive(false);
difStatesOfTree[1].gameObject.SetActive(false);
difStatesOfTree[2].gameObject.SetActive(true);
difStatesOfTree[3].gameObject.SetActive(false);
Debug.Log("difStatesOfTree Level 2");
}
if (pointsToNextState >= 10)
{
difStatesOfTree[0].gameObject.SetActive(false);
difStatesOfTree[1].gameObject.SetActive(false);
difStatesOfTree[2].gameObject.SetActive(false);
difStatesOfTree[3].gameObject.SetActive(true);
Debug.Log("upGrades Level 3");
}
Correct me if Im wrong but if your Trees are being generated from a prefab than attaching this script to the prefab would allow you to do what you are looking for. Id have each difStatesOfTree[] as a child to an empty with the script, each child set to inactive in the inspector. Simply add a function for chop to affect the pointsToNextState. This is a rendition of one of my scripts for a project im working hope this helps.
Answer by RKrusty22 · Jan 13, 2018 at 10:25 PM
Also wish this was simpler or easier to access/change, I need my tiles to change sprites in a sequence upon interaction, to other specific sprites in specific positions. Was much easier to code with a plug-in (STE) but I was hoping to use the native Unity tilemaps.
Your answer
Follow this Question
Related Questions
I can't see tiles after Instantiate() a tilemap 1 Answer
Tilemap tile animation - How to get the current sprite ( or frame ) name with the GetTile command? 0 Answers
How can I add script to a single tile ? 3 Answers
Obtain Sprite Data from Tile in a Grid Using Tile Coordinates 1 Answer
Help with tile palette(Genuine Struggle) 0 Answers