Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Prefab Grass GameObject onto Tile

Discussion in '2D' started by cmiller241, Aug 2, 2022.

  1. cmiller241

    cmiller241

    Joined:
    Aug 1, 2022
    Posts:
    3
    New Unity user here. I am trying to find all instances of a particular tile in unity, and then preFab a grass object in the same location as the particular tile. But when I run the code, the grass appears about 8 tiles right and four tiles up from the location they should be at. 8 tiles right seems to be half the tilemap horizontally, and four tiles up appears to be half the tilemap vertically. Why is script offsetting the gameObjects by half the tilemap. What am I doing wrong? I don't have the best sense of tileMap/worldMap:


    Code (CSharp):
    1. {
    2.     [SerializeField]
    3.     private Tilemap tilemap;
    4.  
    5.     [Header("Grass Prefab")]
    6.     [SerializeField]
    7.     private GameObject grassPrefab;
    8.  
    9.     void Start()
    10.     {
    11.         tilemap.CompressBounds();
    12.         BoundsInt bounds = tilemap.cellBounds;
    13.         TileBase[] allTiles = tilemap.GetTilesBlock(bounds);
    14.  
    15.         for (int x = 0; x < bounds.size.x; x++)
    16.         {
    17.             for (int y = 0; y < bounds.size.y; y++)
    18.             {
    19.                 TileBase tile = allTiles[x + y * bounds.size.x];
    20.                 if (!string.IsNullOrEmpty(tile?.name))
    21.                 {
    22.                     if (tile.name.Equals("sprites_64")) //This is the tile I want to add grass to.
    23.                     {
    24.                         Vector3Int localPlace = new Vector3Int(x, y, 0);
    25.                         Vector3 place = tilemap.CellToWorld(localPlace);
    26.                         Instantiate(grassPrefab, place, Quaternion.identity);
    27.                         Debug.Log("x:" + x + " y:" + y + " worldx:" + place.x + " worldy:" + place.y + " tile:" + tile?.name);
    28.                     }
    29.                 }
    30.                 Debug.Log("x:" + x + " y:" + y + " tile:" + tile?.name);
    31.             }
    32.         }
    33.     }
    34. }
     
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    Is your tilemap's origin at 0,0,0 ?

    CellToWorld may not adjust for any offset (tile anchor position). Try
    GetCellCenterWorld instead.
     
    cmiller241 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    I'm guessing it's something like what @vonchor suggests above, but here is how you can find out:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.
     
  4. cmiller241

    cmiller241

    Joined:
    Aug 1, 2022
    Posts:
    3
    Thank you Voncher. I didn't realize that the GRID was centered at (0,0) in my viewscreen (I didn't really realize how the GRID worked at all). My script was iterating through the tiles, but only relative to the bounds of the tilemap, and not the grid. The result was that the grass objects were being prefabbed in respective distance from one another, just shifted (again because the tilemap originated negative of 0,0 on the grid). Adjusting the tilemap so that its origins are (0,0) made the prefabs spawn correctly.

    That being said, are the Tilemap coordinates respective to itself (starting at (0,0) and ending at (tilemapWidth, tilemapHeight)) or do the Tilemap coordinates equate to the parent Grid (so that the tilemap array could start at, say, (-5,-10))? And if the latter, isn't CellToWorld's purpose to change tileMap coordinates into the Grid coordinates?
     
  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The latter is correct.

    There is a starting position stored in
    tilemap.cellBounds
    (https://docs.unity3d.com/ScriptReference/BoundsInt-position.html) which you are not using in your loop above to get the actual cell position of each Tile. You can validate the positions in the bounds using
    bounds.allPositionsWithin
    (https://docs.unity3d.com/ScriptReference/BoundsInt-allPositionsWithin.html).

    An alternative to instantiating the GameObject for each Tile manually would be to set a GameObject in the sprites_64 Tile Asset itself (https://docs.unity3d.com/ScriptReference/Tilemaps.Tile-gameObject.html), assuming it is a standard Tile. The Tilemap would then instantiate the GameObject for you at the Tile's position.
     
    cmiller241 likes this.
  6. cmiller241

    cmiller241

    Joined:
    Aug 1, 2022
    Posts:
    3
    ChuanXin: Wow.. so helpful. Thank you for your work.