# Variable Not In Scope

**URL:** <https://forums.unrealengine.com/t/variable-not-in-scope/341476>\
**Category:** UI\
**Tags:** variables, scope, unreal-engine, level, Widget, question, Blueprint\
**Created:** [December 14, 2015, 11:08pm UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476 "2015-12-14T23:08:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 14, 2015, 11:08pm UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/1 "2015-12-14T23:08:00Z")

</div>

I would like a button pressed to set a variable to some integer and have it be accessible inside the level blueprint. However when I try this I always get the error that the variable is not in scope. Any help?

Note I set the variable inside the widget event graph using EventPressed (button)

Then in the level blueprint I get a WidgetBP reference, drag off and get Variable then include that in my sequence of nodes.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ecb5cc49](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ecb5cc49](https://forums.unrealengine.com/u/anonymous_user_ecb5cc49)\
**Post date:** [December 15, 2015, 1:51am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/2 "2015-12-15T01:51:21Z")

</div>

This is all about ownership of the variable. If it’s related to the state of the game then it should go in a game state BP and then you can get it anywhere (with the `GetGameState` BP node). If it belongs to an actor in your world then you need to get that actor in the level bp (with the `FindAllActorsOfClass` BP node or hold a ref in the level BP if it’s always in the world). If it’s level specific then it should live inside the level bp but you shouldn’t wire it up so the things know about the level, the knows about everything and nothing should know about the level.

Variables represent state of an object so you need to know what the variable means for the game itself and the object it belongs to and make sure that relationship makes sense.

If you provide some more details I would be happy to give some more insight on this.

---

<div class="post-metadata">

**Author:** ![mindfane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mindfane/32/710746_2.png) [@mindfane](https://forums.unrealengine.com/u/mindfane)\
**Post date:** [December 15, 2015, 3:01am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/3 "2015-12-15T03:01:48Z")

</div>

Can you show us your Blueprints?

You could try making the said variable public and editable outside. That way you can access/change it from another blueprint. Another options would be to create a function in your widget blueprint to get the value of the variable in question. Then you can call that function from another blueprint.

---

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 15, 2015, 9:43pm UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/4 "2015-12-15T21:43:27Z")

</div>

![70766-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/7/1/f71f3624db4aa15208e748ae386d514d58de077e.png)

 ![70767-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/7/8/e787743ff0125e2d4c30f6aaa09d97b42c3534ed.png)

---

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 16, 2015, 2:03am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/5 "2015-12-16T02:03:53Z")

</div>

I had tried making the variable editable by clicking the eye, also a function did not work here either ):

---

<div class="post-metadata">

**Author:** ![mindfane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mindfane/32/710746_2.png) [@mindfane](https://forums.unrealengine.com/u/mindfane)\
**Post date:** [December 16, 2015, 2:49am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/6 "2015-12-16T02:49:10Z")

</div>

I do not find anything wrong with your setup. But try these and let me know what you get

In your level blueprint, before accessing the variable ‘W’ of your ‘Widget Reference’, do a validity check. ie Use an ‘Is Valid’ node and pass ‘Widget Reference’ as its parameter. Connect the rest of your network to the ‘Is Valid’ output. Attach a Print string node to the ‘Not Valid’ output. Now run it and tell us what you get.

I am trying to make sure your Widget Reference is actually pointing to an object that actually exist.

---

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 16, 2015, 3:00am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/7 "2015-12-16T03:00:37Z")

</div>

It returns the print statement, so it is not valid.

Is it because I set the value of W in a separate level? When I go to the next level maybe that value does not exist, or remains unset after the level transfer.

I tried using a function w/ output integer:

 ![70796-untitled.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/7/8/37876fe5d5e6f1ee021428a794600b52efce2e5a.png)

still same results.

If I try using a CastTo node, I get an error and I believe it’s because of a UE4 update, changing the way casting functions:

 ![70798-untitled.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/e/8/ae87fb85fe6bbd77c6c0d86cc8f1f084725d0d80.png)

---

<div class="post-metadata">

**Author:** ![mindfane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mindfane/32/710746_2.png) [@mindfane](https://forums.unrealengine.com/u/mindfane)\
**Post date:** [December 16, 2015, 4:08am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/8 "2015-12-16T04:08:33Z")

</div>

Its not because W is out of scope. The problem is the variable ‘Widget Reference’ does not point to an instance. Same goes for Cast; you’ll need a valid reference before you can cast it.  
Can you show me how you get a reference to your widget and assign it to the variable ‘Widget Reference’?

These threads might be useful

> **[How can i get a variable from a widget blueprint and be able to change the...](https://forums.unrealengine.com/questions/146201/how-can-i-get-a-variable-from-a-widget-blueprint-a.html)**
>
> basically what the title says how can i get a variable from a widget blueprint and be able to change the value in the level blueprint so say i want to change the amount of a value when the level plays such as using the event begin play?

> **[Accesing variables from a widget blueprint to a level blueprint](https://forums.unrealengine.com/questions/126826/accesing-variables-from-a-widget-blueprint-to-a-le.html)**
>
> I want to access a boolean variable from a widget blueprint and implement it into a level blueprint so a cutscene and some music will play when the play button is pressed at any time.

---

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 16, 2015, 4:13am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/9 "2015-12-16T04:13:55Z")

</div>

![70813-a1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/c/6/0c6b22c19531b09270bd581cb78f4ed5831e98ca.png)

---

<div class="post-metadata">

**Author:** ![mindfane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mindfane/32/710746_2.png) [@mindfane](https://forums.unrealengine.com/u/mindfane)\
**Post date:** [December 16, 2015, 5:21am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/10 "2015-12-16T05:21:23Z")

</div>

Well that is not going to work. You created a variable of type ‘Widget BP’. All it means is this variable can contain an instance of WidgetBP. BUT it is still empty, you need to put something (an instance of WidgetBP) inside it for it to work.

What you should do is Create an instance of WidgetBP by spawning and then set the spawned widget instance to ‘Widget Reference’. I am assuming that you spwn the widget from your Level BP (possibly in BeginPlay). If that is not the case and you spawn the widget from some other BP (Pawn for instance) then you should find a way to first get a reference to that BP and then get reference to your Widget from it.

---

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 16, 2015, 4:25pm UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/11 "2015-12-16T16:25:53Z")

</div>

I spawn the widget in a separate level using EventBeginPlay. Would the reference be the return value of the CreateWidgetBPWidget node?

---

<div class="post-metadata">

**Author:** ![mindfane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mindfane/32/710746_2.png) [@mindfane](https://forums.unrealengine.com/u/mindfane)\
**Post date:** [December 17, 2015, 5:39am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/12 "2015-12-17T05:39:37Z")

</div>

Yes, the output of CreateWidgetBPWidget is the actual instance. You should use it to reference your Widget. You could put it inside GameState as it is accessible everywhere.

---

<div class="post-metadata">

**Author:** ![nmm131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nmm131](https://forums.unrealengine.com/u/nmm131)\
**Post date:** [December 19, 2015, 3:35am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/13 "2015-12-19T03:35:45Z")

</div>

Could you show me a blueprint of how to solve this? I can’t get it done.

---

<div class="post-metadata">

**Author:** ![mindfane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mindfane/32/710746_2.png) [@mindfane](https://forums.unrealengine.com/u/mindfane)\
**Post date:** [December 21, 2015, 3:31am UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/14 "2015-12-21T03:31:16Z")

</div>

I could not do that without knowing more about your setup.

But I could give a sample setup here.

1. In your Pawn/PlayerCharcter BP (Lets call it MyPawn), create a variable of type WidgetBP. Make it public. Lets call it WidgetRef
2. When you spawn your Widget, get a reference to your PlayerCharcter thruogh GetPlayerCharcter. Cast the result to ‘MyPawn’ (or whatever class your PlayerCharcter is). Now set the output of ‘CreateWidgetBPWidget’ as the value for MyPawn.WidgetRef
3. Now whenever you want to access the Widget, you should get a reference to your PlayerCharacter using GetPlayerCharcter, Cast it to MyPawn and then access the WidgetRef. To make sure it works all the time, you should do a ‘IsValid’ check on WidgetRef before accessing its memebers.

---

<div class="post-metadata">

**Author:** ![9robin3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@9robin3](https://forums.unrealengine.com/u/9robin3)\
**Post date:** [June 19, 2017, 10:42pm UTC](https://forums.unrealengine.com/t/variable-not-in-scope/341476/15 "2017-06-19T22:42:06Z")

</div>

Im also getting some troubles with this error/warning and it makes mty meshes move to random places in the world/level. How can I solve this?

Im having an actor reference in my player BP that represents any carried object.
