Battery Collector tutorial UBoxComponent error

Hello all, I am new to unreal engine 4 and was therefore watching the videos of the battery collector tutorial to get familiar with C++. I got to video 6 but now I unfortunately keep getting errors when compiling so I am kinda stuck in the tutorial already. Just to be sure I included the code I wrote but I feel like the problem is not due to a writing mistake of mine. I keep receiving the error: cannot convert UBoxComponent to USceneComponent. Earlier in the tutorial we already had to use UStaticMeshComponent and when I change the UBoxComponent in the script below, and the header file, it actually works. Why does UBoxComponent not work in my case but shows to work fine in the tutorial? Thanks for all the help :slight_smile:

First part of SpawnVolume.cpp file:

#include "SpawnVolume.h"
#include "Kismet/KismetMathLibrary.h"
#include "Pickup.h"
    
// Sets default values
ASpawnVolume::ASpawnVolume()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	WhereToSpawn = CreateDefaultSubobject<UBoxComponent>(TEXT("WhereToSpawn"));
	RootComponent = WhereToSpawn;

	SpawnDelayRangeLow = 1.0f;
	SpawnDelayRangeHigh = 4.5f;

}

SpawnVolume.h file:

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpawnVolume.generated.h"

UCLASS()
class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASpawnVolume();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	FORCEINLINE class UBoxComponent* GetWhereToSpawn() const { return WhereToSpawn; }

	UFUNCTION(BlueprintPure, Category = "Spawning")
		FVector GetRandomPointInVolume();

protected:
	UPROPERTY(EditAnywhere, Category = "Spawning")
		TSubclassOf<class APickup> WhatToSpawn;

	FTimerHandle SpawnTimer;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
		float SpawnDelayRangeLow;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
		float SpawnDelayRangeHigh;

private:
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))
		class UBoxComponent* WhereToSpawn;

	void SpawnPickup();

	float SpawnDelay;
};
1 Like

4.16 changed the way you manage include files.

Add:

#include "Components/BoxComponent.h"

4.16 change: Include What You Use:

https://docs.unrealengine.com/latest/INT/Programming/UnrealBuildSystem/IWYUReferenceGuide/

1 Like

Thanks a lot. Everything is working right now so Iā€™m happy that I can continue with the turorial again :slight_smile:

Can double confirm that it works for UE4.17.0, thank you!

Works in 4.18 too - Thanks!