# \[SOLVED\] 4.27 Actors spawning on parts of grid that they shouldn't

**URL:** <https://forums.unrealengine.com/t/solved-4-27-actors-spawning-on-parts-of-grid-that-they-shouldnt/272947>\
**Category:** C++\
**Tags:** unreal-engine, UE4-27\
**Created:** [January 16, 2022, 11:56pm UTC](https://forums.unrealengine.com/t/solved-4-27-actors-spawning-on-parts-of-grid-that-they-shouldnt/272947 "2022-01-16T23:56:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DekuNut](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekuNut](https://forums.unrealengine.com/u/DekuNut)\
**Post date:** [January 16, 2022, 11:56pm UTC](https://forums.unrealengine.com/t/solved-4-27-actors-spawning-on-parts-of-grid-that-they-shouldnt/272947/1 "2022-01-16T23:56:30Z")

</div>

**EDIT**  
I’m the biggest dummy in the world. Look at the code:

> if (TileToSpawn = GrassTile) {

I didn’t put double equals. 🤦‍♂️

* * *

Followed a tutorial on how to create a grid out of hexagons, and it worked smashingly. Then I wanted to spawn trees on some of the tiles.

Quick note, there are two tiles [Grass, Water] and I only want the trees to spawn on the grass. I figured when spawning the tiles, I could just check if the code was going to spawn a grass tile or a water tile, and if it was grass then spawn a tree too. Trees are spawning on water tiles as well though, so clearly I’ve misunderstood the code.

```auto
HexGrid2DArray.SetNumZeroed(GridWidth);
	HexTreeArray.SetNumZeroed(GridWidth); //My code
	for (int32 i = 0; i < HexGrid2DArray.Num(); i++) {
		HexGrid2DArray[i].SetNumZeroed(GridHeight);
	}

	for (int32 y = 0; y < GridHeight; y++) {
		for (int32 x = 0; x < GridWidth; x++) {
			const bool oddRow = y % 2 == 1;
			const float xPos = oddRow ? (x * TileHorizontalOffset) + OddRowHorizontalOffset : x * TileHorizontalOffset;
			const float yPos = y * TileVerticalOffset;

			TSubclassOf<AHexTile> TileToSpawn = GrassTile;
			if (FMath::RandRange(0.f, 1.f) <= ChanceOfWater) {
				TileToSpawn = WaterTile;
			}

			AHexTile* NewTile = GetWorld()->SpawnActor<AHexTile>(TileToSpawn, FVector(FIntPoint(xPos, yPos)), FRotator::ZeroRotator);
			NewTile->TileIndex = FIntPoint(x, y);
			NewTile->SetActorLabel(FString::Printf(TEXT("%d, %d"), x, y));
			HexGrid2DArray[x][y] = NewTile;

//All my code below
			if (TileToSpawn = GrassTile) {
				if (FMath::RandRange(0.f, 1.f) <= ChanceOfTree) {
					TSubclassOf<AHexTree> TreeToSpawn = PineTree;
					const float zPos = 35.0;
					AHexTree* NewTree = GetWorld()->SpawnActor<AHexTree>(TreeToSpawn, FVector(xPos, yPos, zPos), FRotator::ZeroRotator);
					NewTree->TreeIndex = FIntPoint(x, y);
					HexTreeArray.Add(NewTree);
				}
			}
		}
	}

```

This is basically all the code in the file at the moment. Everything works perfectly, except trees are spawning on water tiles, and I’m not sure what I’ve misunderstood.

---

<div class="post-metadata">

**Author:** ![Natalo77](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Natalo77](https://forums.unrealengine.com/u/Natalo77)\
**Post date:** [January 17, 2022, 8:21am UTC](https://forums.unrealengine.com/t/solved-4-27-actors-spawning-on-parts-of-grid-that-they-shouldnt/272947/2 "2022-01-17T08:21:15Z")

</div>

Rubber Duck Debugging.

If writing this post made you see the mistake, then the forum has done its job 😃
