- Home /
Not understanding how to Instantiate gameobjects on a Tilemap. ,Questions about instantiating objects on a Tilemap (Unity 2D)
Hello, I'm working on my first real Unity game, and while I'm having success in a lot of places, I've run into a problem that I can't seem to fix. I'm trying to instantiate gameobjects on a tilemap, but they instantiate to the center of the tilemap's parent object (with a little bit of variation). In addition, when the objects are parented to a tilemap, they don't show up. I can manipulate them in the Inspector, but I can't actually see the gameobjects.
Here's the code I've written to instantiate the gameobject and parent it to a tilemap
public GameObject ModifyPawn(Army army)
{
GameObject pawn = Instantiate(Pawn, tilemap.transform);
TextMeshProUGUI armyLabel = pawn.transform.FindDeepChild("ArmySize").GetComponent<TextMeshProUGUI>();
armyLabel.text = army.GetTotalUnits().ToString();
MilitaryHandler.ModifyMoraleBar(pawn, army);
return pawn;
}
Here's the code I've written to change the position of the gameobject
public void SpawnMilitaryPawn(Army army, TileInfoBase tileToSpawnAt)
{
GameObject pawn = ModifyPawn(army);
Vector3Int tile = new Vector3Int(tileToSpawnAt.X, tileToSpawnAt.Y, 0);
Vector3 coord = tilemap.GetCellCenterWorld(tile);
pawn.transform.position = coord;
}
Here's a picture of the hierarchy that includes the military pawns
And here's a picture of the objects being invisible but still instantiating to the center
any help is appreciated.
Answer by SnailPeople · Nov 08, 2021 at 02:36 AM
Hey everyone, I fixed this problem. It turns out the gameobjects were too large and were somehow being positioned according to the bottom left corner, which due to their large size, caused them to be near the center of the screen.
Your answer
Follow this Question
Related Questions
why Lagging from tilemap 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to use tilemaps? 0 Answers