# Basic Light rotation not working in viewport/preview, yet works in standalone

**URL:** <https://forums.unrealengine.com/t/basic-light-rotation-not-working-in-viewport-preview-yet-works-in-standalone/478494>\
**Category:** Rendering\
**Tags:** basics, play-in-editor, unreal-engine, question\
**Created:** [February 9, 2021, 7:31am UTC](https://forums.unrealengine.com/t/basic-light-rotation-not-working-in-viewport-preview-yet-works-in-standalone/478494 "2021-02-09T07:31:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_5bb8f3a4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5bb8f3a4](https://forums.unrealengine.com/u/anonymous_user_5bb8f3a4)\
**Post date:** [February 9, 2021, 7:31am UTC](https://forums.unrealengine.com/t/basic-light-rotation-not-working-in-viewport-preview-yet-works-in-standalone/478494/1 "2021-02-09T07:31:25Z")

</div>

I have an issue where I set the directional light rotation on Tick. Yet in the play window (Play \> New Editor Window) the light does not change, but when I switch to the viewport and I change the camera position (while still playing in editor window), the light jumps to current rotation . The light is set to movable. **When I run the standalone game (Play \> Standalone Game) the rotation works as expected.**

I get the light in BeginPlay:

```
for (TObjectIterator<ADirectionalLight> Itr; Itr; ++Itr) {
   if (Itr->GetName().Equals("SunLight")) {
        SunLightRef = *Itr;
        break;
    }
}

```

and then on Tick i do:

```
SunLightRef->SetActorRelativeRotation(CurrentSunRotation);

```

Specs:  
Windows 10  
i7-4790k 4.00GHz  
32Gb Ram  
AMD R9 390x

---

<div class="post-metadata">

**Author:** ![YedesCodes](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/yedescodes/32/981996_2.png) [@YedesCodes](https://forums.unrealengine.com/u/YedesCodes)\
**Post date:** [February 12, 2021, 5:57am UTC](https://forums.unrealengine.com/t/basic-light-rotation-not-working-in-viewport-preview-yet-works-in-standalone/478494/2 "2021-02-12T05:57:26Z")

</div>

I think your object iterator is causing a problem, you can try this code :  
This code is in the BeginPlay

```
TArray<AActor*> FoundLights;
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), ADirectionalLight::StaticClass(), FoundLights);

	for (AActor* TActor : FoundLights)
	{
		if (TActor->GetName().Equals("SunLight"))
		{
			MySunLight = Cast<ADirectionalLight>(FoundLights[0]);
			break;
		}
	}

```

and this one in your tick :

```
if (IsValid(MySunLight))
	{
		FRotator MyCurrentRot = MySunLight->GetActorRotation();
		MySunLight->SetActorRelativeRotation(CurrentSunRotation);
	}
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5bb8f3a4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5bb8f3a4](https://forums.unrealengine.com/u/anonymous_user_5bb8f3a4)\
**Post date:** [February 12, 2021, 5:31pm UTC](https://forums.unrealengine.com/t/basic-light-rotation-not-working-in-viewport-preview-yet-works-in-standalone/478494/3 "2021-02-12T17:31:51Z")

</div>

That worked thank you!
