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)?
Answer by nopogo · Feb 21, 2018 at 03:38 PM
Updating to the latest version of unity resolved my errors
Answer by rolographic · Nov 20, 2018 at 09:53 PM
It was hard to find but you can fix that behavior by unchecking the Tilemap component on the Layer1 inside the palette.
Answer by FDT · Feb 18, 2018 at 11:23 AM
You have to instantiate the gameobject first.
I've tried doing this, the result is an instantiated object at origin, then a copied version of this object at the correct location. So either that is the way to go and im doing something wrong, or this is and im missing something (or the linked gameobject in the tilesystem was never intended for this purpose)
I'm even more confused now, : according to Unity's own documentation im setting the gameobject correctly
Answer by Salleyyyyy · Apr 03, 2020 at 07:52 PM
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;
}
}
It is important to say that i am using Unity version 2019.3.7f1, as since this version the cloned gameObject is placed in the middle of the tile.
PS: This is a solution without a prefab. If you have a prefab, you should not instantiate it, use e.g. a public attribute and drag and drop the prefab. I think there shouldn't be a problem as far as you don't instantiate it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to check if a position has a type of tile 1 Answer
Can't Destroy Transform Component Error after exiting Play mode 2 Answers
How can I Remove one tile from tilemap 1 Answer
Problem with 2D tiles from a tilemap appearing blurry in game 0 Answers
When I flip my sprite it goes out of the tilemaps foreground 0 Answers