Expression Must Be A Modifiable lvalue

Trying to work on a light switch event but I am getting an error for the line, “PointLight1->ToggleVisibility() = true;” that says “Expression must be a modifiable lvalue.” Code is below.

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class CLASSPROJECT_JEREMY_API AFlashlight : public AActor
{
	GENERATED_BODY()

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

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

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

	// Point light component
	UPROPERTY(VisibleAnywhere, Category = "Switch Components")
		class UPointLightComponent* PointLight1;

	// Sphere Component
	UPROPERTY(VisibleAnywhere, Category = "Switch Components")
		class USphereComponent* Sphere1;

	// Property to adjust light intensity
	UPROPERTY(VisibleAnywhere, Category = "Switch Variables")
		float DesiredIntensity;

	// Function to call when something enters sphere
	UFUNCTION()
		void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor,
			class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	// Function to call when something leaves sphere
	UFUNCTION()
		void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor,
			class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

	// Toggle light component's visibility
	UFUNCTION()
		void ToggleLight();

};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "ClassProject_Jeremy.h"
#include "Components/PointLightComponent.h"
#include "Components/SphereComponent.h"
#include "Flashlight.h"
#include "DrawDebugHelpers.h"

// Sets default values
AFlashlight::AFlashlight()
{
	DesiredIntensity = 3000.0f;

	PointLight1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLight1"));
	PointLight1->Intensity = DesiredIntensity;
	PointLight1->ToggleVisibility() = true;
	RootComponent = PointLight1;

	Sphere1 = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere1"));
	Sphere1->InitSphereRadius(250.0f);
	Sphere1->SetupAttachment(RootComponent);

	Sphere1->OnComponentBeginOverlap.AddDynamic(this, &AFlashlight::OnOverlapBegin);
	Sphere1->OnComponentEndOverlap.AddDynamic(this, &AFlashlight::OnOverlapEnd);
}


// Called when the game starts or when spawned
void AFlashlight::BeginPlay()
{
	Super::BeginPlay();
	
	DrawDebugSphere(GetWorld(), GetActorLocation(), 250.f, 50, FColor::Green, true, -1, 0, 2);
}

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

}

void AFlashlight::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor,
	class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		ToggleLight();
	}
}

void AFlashlight::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor,
	class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		ToggleLight();
	}
}

void AFlashlight::ToggleLight()
{
	PointLight1->ToggleVisibility();
}

By using ToggleVisibility(), you are calling a method, rather than getting a property to set a value to.

ToggleVisibility gets the current visibility and sets to the opposite, so if it was true, new visibility will be false

UPointLightComponent* Light;
Light->ToggleVisibility(); // true to false | false to true

If you want to set the visibility using a boolean to control it, you can use:

UPoingLightComponent* Light;
Light->SetVisibility(BoolValue);

In the future, feel free to check the documentation API, as you can check the arguments and values for any class or function. :slight_smile: