# Object disappears between Setup() and Draw()?

**URL:** https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911
**Category:** Coding Questions
**Created:** [June 25, 2021, 6:35pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911 "2021-06-25T18:35:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pbissell](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@pbissell](https://discourse.processing.org/u/pbissell)
#### Post date: [June 25, 2021, 6:35pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911/1 "2021-06-25T18:35:50Z")

</div>

I’m a little perplexed. Is there anything that happens between Setup() and Draw() in the sketch?

This is my 20th or so ‘mover’ based project. No arrays on this one, but a basic Mover class (position, velocity, radius, etc).  
In the code below: the first call to console.log(mover) in Setup(); shows me all the created object’s details in the console - like you would expect. It is a valid object with parameters, etc.  
The second call to console.log(mover) comes back as **undefined**.  
How could I have lost my mover object between Setup() and Draw().

I have done a variation where the class declaration is on the script.js page along with Setup() and Draw(). Same thing happens - I have an object in Setup() which is gone missing by Draw(). How could I have lost the mover object between Setup() and Draw().

Thanks, Paul

```auto
let mover;

function setup() {
  createCanvas(400, 400);
  let mover = new Mover(200, 200);
  console.log(mover);
}

function draw() {
  background(50);
  console.log(mover);
}

```

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [June 25, 2021, 7:14pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911/2 "2021-06-25T19:14:47Z")

</div>

Hello, @pbissell, and welcome to the Processing Forum!

You have this in your `setup()` function definition:

```auto
  let mover = new Mover(200, 200);

```

With `let`, it defines `mover` as local variable in the function. Outside the function, `mover` refers to a global variable by that name that you declared prior to the `setup()` function. Though it was declared, it was not initialized with an object.

Inside `draw()`, you access the undefined global variable.

_Edited on June 25, 2021 to clarify the explanation._

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [June 25, 2021, 7:26pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911/3 "2021-06-25T19:26:51Z")

</div>

> [@pbissell](#):
>
> `let mover = new Mover(200, 200);`

**And to easily rectify,** just delete the “let” inside setup so that line reads:  
`mover = new Mover(200, 200);`  
🤓

---

<div class="post-metadata">

### Author: ![pbissell](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@pbissell](https://discourse.processing.org/u/pbissell)
#### Post date: [June 25, 2021, 7:59pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911/4 "2021-06-25T19:59:16Z")

</div>

OMG. Thank you so much. I double Let!

---

<div class="post-metadata">

### Author: ![pbissell](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@pbissell](https://discourse.processing.org/u/pbissell)
#### Post date: [June 25, 2021, 8:00pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911/5 "2021-06-25T20:00:19Z")

</div>

Wow. Thank you so much. I double Let and couldn’t see it. Thank you!

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [June 25, 2021, 8:30pm UTC](https://discourse.processing.org/t/object-disappears-between-setup-and-draw/30911/6 "2021-06-25T20:30:49Z")

</div>

> [@pbissell](#):
>
> … double Let and couldn’t see it.

See [p5.js: Variable Scope](https://p5js.org/examples/data-variable-scope.html).

It is OK to have global and local variables with the same name, so long as you are careful. For example, you might write a function with many lines of code and some local variables. Make sure those variables are declared properly within the function, using `let`. Then, if you decide later on to use the function within a longer script, you will not need to be concerned about whether that script contains global variables with the same names as the local variables within the function.
