Accessing int32 variable crashes the game

I have a grid that I use as a map. I set an int value for the width and height but when I try to read these variables the game crashes.

If I try to print out the value just after setting it the game does not crash but from another function it will.

Declarations:

	UPROPERTY(EditAnywhere)
		int32 MapWidth;

	UPROPERTY(EditAnywhere)
		int32 MapHeight;

Functions:

void UGameMap::Generate(int32 Width, int32 Height)
{
	MapWidth = Width;
	MapHeight = Height;

   	Print(FString::FromInt(MapWidth)); //does not crash the game

	for (size_t x = 0; x < 50; x++)
	{
		for (size_t y = 0; y < 50; y++)
		{
			FWorldTile Tile;
			WorldTiles.Add(Tile);
		}
	}
}

bool UGameMap::InMapBounds(FVector2D TilePosition, FVector2D BuildingSize)
{
	Print(FString::FromInt(MapWidth)); //crashes the game

	if (TilePosition.X < 0 || TilePosition.X + BuildingSize.X > 50)
		return false;
		//|| TilePosition.X + BuildingSize.X > MapWidth || TilePosition.Y < 0 || TilePosition.Y + BuildingSize.Y > MapHeight)
	//	return false;

	return true;
}

Logging these issues you should add a callstack to make it a bit easier. Have you check what the value MapWidth is with a debugger just before the crash? Are you 100% sure it’s initialized?

Looking at the implementation FString::FromInt it could (??) crash but I doubt it after testing it out.

Also how do you create those object classes? they are reference them somewhere afterwards? There possibility they get garbage collected, which may create invalid pointers and invalid “this”. I this case function code will run but will crash in first attempt to access local object variables as this pointer will be invalid.

Turns out I forgot to set the pointer I am trying to access to an object.

Forgot to set object as a variable so it was a nullptr.

Hey! Can you elaborate on this further? I don’t see any object, and Im having the same issue.