# Script variables not sticking

**URL:** <https://discussions.unity.com/t/script-variables-not-sticking/651682>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [January 11, 2017, 8:58pm UTC](https://discussions.unity.com/t/script-variables-not-sticking/651682 "2017-01-11T20:58:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![P2B\_Gaming](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@P2B\_Gaming](https://discussions.unity.com/u/P2B_Gaming)\
**Post date:** [January 11, 2017, 8:58pm UTC](https://discussions.unity.com/t/script-variables-not-sticking/651682/1 "2017-01-11T20:58:52Z")

</div>

Hey Guys,

I’m working on a 2D game, and have a script to hide a door sprite when the player opens the door.

I’ve attached the door sprite to the script variable in the editor, but when I preview the game the variable is empty.

This is edit mode:  
 ![2917039--215256--Edit.png](https://europe1.discourse-cdn.com/unity/original/4X/8/f/3/8f345b8c196e2683bcdf704c7c3075d49553f28f.png)

Then when I hit the play button to preview the scene:  
 ![2917039--215257--Run.png](https://europe1.discourse-cdn.com/unity/original/4X/0/1/a/01a2086f2146acfd42cd4621ed676aaf2a3adbf5.png)

When I’m running the game, I can re-attach the door sprite and the script works, but when I preview it again, the variable is empty again.

---

<div class="post-metadata">

**Author:** ![Brathnann](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@Brathnann](https://discussions.unity.com/u/Brathnann)\
**Post date:** [January 11, 2017, 9:07pm UTC](https://discussions.unity.com/t/script-variables-not-sticking/651682/2 "2017-01-11T21:07:57Z")

</div>

Are you sure you’re not trying to do something in code that overwrites the sprite? I would suggest showing some code.

---

<div class="post-metadata">

**Author:** ![P2B\_Gaming](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@P2B\_Gaming](https://discussions.unity.com/u/P2B_Gaming)\
**Post date:** [January 11, 2017, 9:36pm UTC](https://discussions.unity.com/t/script-variables-not-sticking/651682/3 "2017-01-11T21:36:32Z")

</div>

> [@Brathnann](#):
>
> Are you sure you’re not trying to do something in code that overwrites the sprite? I would suggest showing some code.

That fixed it, I had some code that I thought initialized the variables, but actually overwrote them, thanks!
