Search code examples
c++graphicsunreal-engine4game-development

UE4: Actor disappears after play is hit


I am using Unreal Engine version 4.25. I have created an actor and placed it on a blank template. The actor is visible in the editor but becomes invisible once the play button is hit. I am trying to move the object in circles with my code.

Here's the code:

MyActor.cpp

#include "MyActor.h"

// Sets default values
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;

    Dimensions = FVector(10, 0, 0);
    AxisVector = FVector(1, 0, 0);
    Location   = FVector(1, 0, 0);
    Multiplier = 50.f;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);



    // Updates the angle of the object
    AngleAxis += DeltaTime * Multiplier;

    if (AngleAxis >= 360.0f)
    {
        AngleAxis = 0;
    }

    //Rotates around axis
    FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector);

    Location.X += RotateValue.X;
    Location.Y += RotateValue.Y;
    Location.Z += RotateValue.Z;

    SetActorLocation(Location, false, 0, ETeleportType::None);

}

MyActor.h

#pragma once

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

UCLASS()
class ROBOTEURS_API AMyActor : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    AMyActor();

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

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

    // declare our float variables
    UPROPERTY(EditAnywhere, Category = Movement)
        float AngleAxis;

    UPROPERTY(EditAnywhere, Category = Movement)
        FVector Dimensions;

    UPROPERTY(EditAnywhere, Category = Movement)
        FVector AxisVector;

    UPROPERTY(EditAnywhere, Category = Movement)
        FVector Location;

    UPROPERTY(EditAnywhere, Category = Movement)
        float Multiplier;

};

What could have gone wrong?


Solution

  • This fixed it, I did not create a mesh: https://www.youtube.com/watch?v=30XEdBoPw6c

    I added the following:

    .cpp

    AMyActor::AMyActor()
    {
        ...
        Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
        RootComponent = Root;
    
        Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
        Mesh->AttachTo(Root);
    
        ...
    }
    

    .h

    public: 
        ...
    
        UPROPERTY()
            USceneComponent* Root;
    
        UPROPERTY(EditAnywhere)
            UStaticMeshComponent* Mesh;
        ...