What is the correct way to add gameobjects to tiles

Right now I based my custom tile on the unity ruletile example from github where I set the gameobject of the tile in the gettiledata class. This correctly places an instance of the prefab as a child of the tilemap in the right possition. It works right??


public override void GetTileData(Vector3Int position, ITilemap tileMap, ref TileData tileData){
			tileData.sprite = m_DefaultSprite;
			tileData.colliderType = m_DefaultColliderType;
			tileData.flags = TileFlags.LockTransform;
			tileData.transform = Matrix4x4.identity;
			tileData.gameObject = tilePrefab;
}

However whenever I start and stop the scene I get the following errors:


Can’t destroy Transform component of ‘ItemPrefab(Clone)’. If you want to destroy the game object, please call ‘Destroy’ on the game object instead. Destroying the transform component is not allowed.


And when I try to build the project the build fails with the message:


Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.


Anybody here knows the correct ways to set custom gameobjects (with scripts) on a tile (either spawned by code with SetTile() or painted in editor)?

You have to instantiate the gameobject first.

Updating to the latest version of unity resolved my errors

It was hard to find but you can fix that behavior by unchecking the Tilemap component on the Layer1 inside the palette.

Screenshot

Hi @nopogo , Here is my solution, if any questions are there, please ask:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.Tilemaps;
using UnityEngine;

/**
    Klasse eines default Tile, auf dem weitere Gegenstände abgebildet werden
**/

public class DefaultTile : Tile
{


    //Sprite für den Boden
    private Sprite default_Sprite; 
    private static GameObject object_test;
    private static bool c = false;

    
    /**
    public override void RefreshTile(Vector3Int position, ITilemap tilemap)
    {
        Debug.Log("Refresh Methode");
        tilemap.RefreshTile(position);
    }
    **/
    
    
    public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go){
        Destroy(object_test);
        return true;
    }
    
    

    /**
        Methodenaufruf, um Informationen zum Rendern des Tiles zu erhalten, wie z.B. das Sprite des Tiles
    **/
    public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    {
        Debug.Log("Methode GetTile");
        this.default_Sprite = Resources.Load<Sprite>("Floor");
        tileData.sprite = this.default_Sprite;

        if(!c){
            object_test = GameObject.CreatePrimitive(PrimitiveType.Cube);
            c = true;
        }
        
        tileData.gameObject = object_test;
    }


}