SphereComponent not showing/spawning?

For some reason, my SphereComponent doesn’t spawn, the actor spawns perfectly fine, but I cannot see the SphereComponent nor interact with its OnOverlapBegin. Can someone identify the problem?

In the header:

class USphereComponent* SphereVolume;

In the cpp:

AMyActor::AMyActor()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	bReplicates = true;
	ActorDestroyTime = 5.0f;

    (.........)
	SphereVolume = CreateDefaultSubobject<USphereComponent>(TEXT("Timewarp Volume"));
	SphereVolume->InitSphereRadius(800.0f);
	SphereVolume->AttachParent = RootComponent;
	SphereVolume->ShapeColor = FColor::;
	SphereVolume->SetVisibility(true);
	SphereVolume->SetIsReplicated(true);
	SphereVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	SphereVolume->SetCollisionResponseToAllChannels(ECR_Ignore);
	SphereVolume->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
	SphereVolume->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
	SphereVolume->OnComponentEndOverlap.AddDynamic(this, &AMyActor::OnOverlapEnd);
}
void AMyActor::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (OtherComp)
		return;

	if (OtherActor->IsA(AMyCharacter::StaticClass()))
	{
		if (OtherActor && (OtherActor != this))
		{
			if (Role > ROLE_Authority)
			{
				//Slow him down
				(AMyCharacter*)(OtherActor);
			}
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, "OVERLAAAAAAAAAAPPPPPPP");
		}
	}
}

void AMyActor::OnOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (OtherActor->IsA(AMyCharacter::StaticClass()))
	{
		if (OtherActor && (OtherActor != this))
		{
			GEngine->AddOnScreenDebugMessage(1, -1.0f, FColor::Green, "STOP OVERLAAPPP");
		}
	}
}

You need need to do RootComponent = SphereVolume;.

To clarify why it’s currently not working is because the RootComponent is never set and is null until you set it.

I think your problem lies in that you dont have an ObjectInitialiser.

If you wish to add subobject to your class, you need to use the GENERATE_UCLASS_BODY() macro instead of the GENERATE_BODY() macro.

Using that new macro, you don,t need to declare the constructor in your header file, the macro takes care of that for you.

Now, in your .cpp, your constructor will look like this:

AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
	SphereVolume= ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("Timewarp Volume"));
}

You dont need to actually use ObjectInitialiser, but I always found it more “explicit”.

Also, having it as your root component is not a bad idea, so what MJLaukala suggested is good, but it not the root of your problem.

I’m not sure what version this changed in but this is no longer the case. You no longer need a reference to the FObjectInitializer to create a DefaultSubobject in the default constructor.

I tried it, had tried it before as well, the problem persists…
RootComponent = SphereVolume;
I actually tried setting up a DebugSphere on the component’s location (so retrieve the component’s location and spawn it there), and it spawns fine, the only problem seems to be the sphere itself isn’t initialized, but should be, that code was taken from the source code.

From the Default projectile class:
AMyProjectile::AMyProjectile()
{
// Use a sphere as a simple collision representation
CollisionComp = CreateDefaultSubobject(TEXT(“SphereComp”));
CollisionComp->InitSphereRadius(5.0f);
CollisionComp->BodyInstance.SetCollisionProfileName(“Projectile”);
CollisionComp->OnComponentHit.AddDynamic(this, &AL2Projectile::OnHit);

whoops. new answer incoming.

  1. USphereComponent does not have a mesh to render. You’ll be able to see it’s bounds in the editor but not while playing.
  2. SphereVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision); is incorrect. You want SphereVolume->SetCollisionEnabled(ECollisionEnabled::QueryOnly); for overlaps. Look here: ECollisionEnabled::Type

Speaking of which, this component defaults to 'ECollisionEnabled::QueryOnly`

Thanks mate, I just copied it from other source code examples to see if it would change anything, I deleted those lines, but still, doesn’t work…

Are you sure you are entering and leaving the sphere? It’s current diameter is equivalent to 16 meters.

also, what is line 9?

Thanks for helping MJLaukala.

Actually I can’t even see the sphere, so I do not know if I am entering or not. Sometimes when I’m walking around the map I see - almost as if randomly - that I am entering it (have a debug message there).
So what I have there currently is:

SphereVolume = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Volume"));
	SphereVolume->InitSphereRadius(1000.0f);
	SphereVolume->AttachParent = RootComponent;
	SphereVolume->ShapeColor = FColor::;
	SphereVolume->SetVisibility(true);
	SphereVolume->SetIsReplicated(true);
	//SphereVolume->AttachTo(RootComponent);
	SphereVolume->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
	SphereVolume->OnComponentEndOverlap.AddDynamic(this, &AMyActor::OnOverlapEnd);
	RootComponent = SphereVolume;

SphereVolume->InitSphereRadius(1000.0f); will make your sphere have a diameter of 20 meters. Also, don’t spawn inside of the sphere. While playing type “show collision” in to the command console. This should render the collision bounds and help you get a better idea of what is going on.

Oh that helped a lot! I can see the volume now, its being spawned correctly - why can’t I see it normally though? Its visibility is set to true, so I should be able to see it.

However, its not registering overlap events nor does it get destroyed when the actor is destroyed. Do I need to override the destroy method?

Edit: Apparently its generating hit events, the default projectile from the Shooter game bounces off from it…

You should be able to see it in the editor but not while playing. That’s just how it works. I’m not exactly sure why it’s not working. What is line 9?

Hello GuilhermeB,

The reason the collision isn’t showing may be due to the Hidden in Game variable. This is separate from the Visible variable as it only affects its visibility at runtime. You should be able to set it to false in C++ as you do with other variables.

Sorry what do you mean by Line 9? Essentially, I showed every code relative to the SphereVolume. Should I post the entire Constructor class?

Thank you , I’m now able to see it. Overlaps are fixed as well.

Now only two problem remain:

  • the volume is generating collision against projectiles, meaning, from the ShooterGame example, the projectiles are hitting the volume and coming back. Any idea on how to disable it?
  • the volume doesn’t get destroyed when the actor is destroyed

If you look at the Collision section of the details panel for this SphereComponent, you can select “Custom” as a preset. If you do this, you can set the Projectile channel to be ignored for collision. If the projectile channel doesn’t exist, you can add your own custom channel under Project Settings → Collision. You can then apply the collision channel to your projectile.

For the volume being destroyed, is it calling Destroy on itself?