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. Dismiss Notice

Question Rule Tiles not placing a prefab - doing something wrong?

Discussion in '2D' started by GameDevSA, Sep 4, 2023.

  1. GameDevSA

    GameDevSA

    Joined:
    Dec 12, 2016
    Posts:
    104
    Using: Unity 2019.4.4 LTS and relative 2D extras package.

    At the moment, the only way I can seem to place prefabs in tilemaps is with the prefab brush. This is not ideal, though, as you cannot erase those prefabs with the default brush, and have to go back to the brush you painted it with. A bit tedious if there are lots of prefabs and different brushes. It's also more convenient to just select the different prefabs from your tile palette as tiles.

    I have tried using Rule Tiles to get around this, as you can select game objects, however, when I place them in the scene, it seems to place them just as sprites, not as game objects. Here's an example of my player:

    upload_2023-9-4_20-53-27.png

    As far as I can tell, I've set it up correctly. I've tried it with and without tiling rules, neither works. I don't really want any tiling rules, I just want to create a tile that is a prefab of my player, and then later other tiles that are other prefabs.

    I'm a bit confused as I have clearly set the game object to something. Thanks for your help.
     
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    You can use a plain Tile. Inspect the tile in the project folder and using the menu at the top right of the inspector switch to debug mode and the game object field will appear. Why it’s this way is beyond me.

    Be sure you have a sprite set in the tile. Drag the tile into a palette. When you paint the tile the game object will appear.
     
  3. GameDevSA

    GameDevSA

    Joined:
    Dec 12, 2016
    Posts:
    104
    upload_2023-9-4_23-55-9.png

    Thanks for your response. Which one do you mean by plain tile?

    It's so weird. Rule tiles say "Game Object", I don't understand why that doesn't work.
     
  4. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    My bad, I for some reason thought you could make a Tile with the create asset menu, but you can't.

    Just drag a sprite into the Palette and you will have a plain tile. What I mean is an asset in the project folder that is of the C# class Tile.

    When you drag in the sprite into the palette, it'll ask you where to put the asset file (in your project somewhere).

    When that's done, go to the place you put the asset that you just created. Right click and use the Properties context menu item (should be near the bottom of the list).

    When you open that, click the three dots near the upper-right of the window and select Debug.

    That will open the GameObject field. As I said, I am unclear why they are hiding the GameObject field.

    Note that the TileFlags have two selections that affect GameObject use with Tiles.

    When you paint the tile the GameObject should appear.

    Now you may find that the tile sprite will interfere visually with the GameObject but that's a different issue.

    Unfortunately, if you leave out the Tile Sprite the Palette will not show a sprite in the Palette. Normal Unity tiles can't fix this issue without some extra coding somewhere, perhaps in the sprite.
     
  5. GameDevSA

    GameDevSA

    Joined:
    Dec 12, 2016
    Posts:
    104
    Thanks you've given me a real clue about the game object... That's given me an idea about how I might be able to make a custom script to fix it...

    I actually realised, the Rule Tile does put a prefab in the tilemap. But, for some reason, in the editor you can not see the prefab in the tilemap heirarchy. But, when you play, it must do some sort of conversion at run time because there's the game object in the scene. It doesn't behave as expected though, as when you move, it leaves behind a sprite ghost. So, it seems to place a sprite and a game object in the tile, and both are left there at run time. So I've been trying to find a way to do a custom script to fix this...

    Sprite ghost example below, from rule tiles not behaving correctly with game objects. Appears to be a bug.

    Sprite Ghost.gif
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    It looks like you have set the Hero Sprite in your Rule on the right side (The box with Select). If you remove it, the "ghost" Sprite should not show up.
     
  7. GameDevSA

    GameDevSA

    Joined:
    Dec 12, 2016
    Posts:
    104
    The problem is, if you only set the game object, but do not set the sprite, as I have here with a new test tile:

    upload_2023-9-8_12-49-41.png

    If you do that, then if you drag the tile onto the Unity Tile Palette, there is no preview for the sprite. It's there on the palette, and you can use it if you can remember which tile to use, but it's invisible.

    So, you have to also set a sprite if you want to have a preview on the tile palette. But, for some reason, this means that when you paint with it, it paints both a sprite and a game object on the tile map, which it shouldn't do.