- Home /
Trouble with tilemaps as prefabs for 2D plattformer
Hi!
I'm new to Unity and am working on an endless runner platformer that changes the level from a list when the player reaches an "end point" of the previous level. I'm following this tutorial by Code Monkey ( https://www.youtube.com/watch?v=NtY_R0g8L8E ) where he creates prefabs that spawn after each other. I tried doing that but instead drawing different levels using a tile palette, saving them as a prefab, and then trying to spawn them. The result is 1 block from the tile palette and nothing else. I'm not sure how to explain any further but my problem is that my drawn level is gone and only one block is spawned from the prefab. Would really appreciate some help!
Picture of regular view:
Picture of prefab view:
Any other suggestions on how to proceed are much appreciated! As noted I'm not aware of any good practices but am eager to learn.
Answer by Dsaxni · Jul 22, 2020 at 08:57 AM
Might have figured out a solution. I created an empty prefab, created a grid and tilemap within it and used the previous tile palette in the prefab view. I had a LevelStart platform that is not a prefab which is the start area for the endless runner. Then I initiated the prefab level part with this code in an empty object:
public class LevelGenerator : MonoBehaviour
{
public Transform LevelPart_Start;
public Transform LevelPart_1;
private void Awake(){
Transform lastLevelPartTransform;
lastLevelPartTransform = SpawnLevelPart(LevelPart_Start.Find("EndPosition").position);
// can copy this part to create more clones
lastLevelPartTransform = SpawnLevelPart(lastLevelPartTransform.Find("EndPosition").position);
}
private Transform SpawnLevelPart(Vector3 spawnPosition){
Transform levelPartTransform = Instantiate(LevelPart_1, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
}
The explanation might be a bit unclear but I hope it helps anyone else with this issue.