# Cannot use array variable defined in setup()

**URL:** <https://discourse.processing.org/t/cannot-use-array-variable-defined-in-setup/32578>\
**Category:** Beginners\
**Created:** [October 3, 2021, 11:54am UTC](https://discourse.processing.org/t/cannot-use-array-variable-defined-in-setup/32578 "2021-10-03T11:54:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dirknbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dirknbr/32/14600_2.png) [@dirknbr](https://discourse.processing.org/u/dirknbr)\
**Post date:** [October 3, 2021, 11:54am UTC](https://discourse.processing.org/t/cannot-use-array-variable-defined-in-setup/32578/1 "2021-10-03T11:54:36Z")

</div>

I want each of 30 lines to have a distinct random colour, but I get a ‘cols cannnot be resolved’ error in the draw() part.

```auto
//https://funprogramming.org/76-Slowly-morphing-bezier-curves.html
void setup() {
  size(500, 400);
  smooth();
  noFill();
  int [][] cols = new int[30][3];
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 3; j++) {
      cols[i][j] = int(random(255));
    }
  }
}
void draw() {
  background(255);
  float t = frameCount / 100.0;
  for (int i = 0; i < 30; i++) {
    stroke(cols[i][0], cols[i][1], cols[i][2]);
    bezier(
      width/2, height, 
      width/2, noise(1, i, t)*height, 
      noise(2, i, t)*width, noise(4, i, t)*height, 
      noise(3, i, t)*width, noise(5, i, t)*height
    );
  }
  // https://processing.org/reference/saveFrame_.html
  saveFrame("line-######.png");
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 3, 2021, 11:58am UTC](https://discourse.processing.org/t/cannot-use-array-variable-defined-in-setup/32578/2 "2021-10-03T11:58:29Z")

</div>

Hi,

You are declaring `cols` inside of `setup()` so it exists only within it. If you want it to be also recognized in `draw()`, then you need to declare it outside of `setup()`.

```auto
int [][] cols;

void setup() {
  size(500, 400);
  smooth();
  noFill();
  cols = new int[30][3];
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 3; j++) {
      cols[i][j] = int(random(255));
    }
  }
}
void draw() {
  background(255);
  float t = frameCount / 100.0;
  for (int i = 0; i < 30; i++) {
    stroke(cols[i][0], cols[i][1], cols[i][2]);
    bezier(
      width/2, height, 
      width/2, noise(1, i, t)*height, 
      noise(2, i, t)*width, noise(4, i, t)*height, 
      noise(3, i, t)*width, noise(5, i, t)*height
    );
  }
  // https://processing.org/reference/saveFrame_.html
  saveFrame("line-######.png");
}

```

If you want more info, try looking for variables scope.

---

<div class="post-metadata">

**Author:** ![dirknbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dirknbr/32/14600_2.png) [@dirknbr](https://discourse.processing.org/u/dirknbr)\
**Post date:** [October 3, 2021, 12:04pm UTC](https://discourse.processing.org/t/cannot-use-array-variable-defined-in-setup/32578/3 "2021-10-03T12:04:10Z")

</div>

Brilliant, thank you, I knew it was simple 🙂
