# P5.js issue - "bullet.push is not a function"

**URL:** https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677
**Category:** Beginners
**Created:** [March 27, 2019, 10:11am UTC](https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677 "2019-03-27T10:11:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![torridger](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@torridger](https://discourse.processing.org/u/torridger)
#### Post date: [March 27, 2019, 10:12am UTC](https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677/1 "2019-03-27T10:12:00Z")

</div>

Hi there,

trying to follow this tutorial by TheCodingTrain on YouTube with some modification:

[![](https://img.youtube.com/vi/biN3v3ef-Y0/hqdefault.jpg "Coding Challenge #5: Space Invaders in JavaScript with p5.js") ](https://www.youtube.com/watch?v=biN3v3ef-Y0)

I keep getting the error “bullet.push is not a function”.

Any help is much appreciated.

Please see my sketch.js code below:

```auto
var character;
var scl = 20;
var projectile = [];
var bullet = [];

function setup() {
  createCanvas(1000, 1000);
  character = new Character();
  bullet = new Bullet(width/2, height/2);
  for (var i = 0; i < 5; i++) {
    projectile [i] = new Projectile(i*80+80, 60);
    
  }
}

function draw() {
  background(0,0,0);
  character.update();
  character.show();
   for (var i = 0; i < bullet.length; i++) {
    bullet[i].show();
    bullet[i].move();
  }
  
  for (var i = 0; i < 5; i++) {
    projectile [i].show();
  }
}

function keyPressed() {
  if (keyCode === LEFT_ARROW) {
    character.dir(-3, -3);
  } else if (keyCode === UP_ARROW) {
    character.dir(3, -3);
  } else if (keyCode === RIGHT_ARROW) {
    character.dir(3, 3);
  } else if (keyCode === DOWN_ARROW) {
    character.dir(-3, 3);
  }
  
if (keyCode === 32) {
       var bullet = new Bullet(character.x, height);
       bullet.push (bullet);
  }

}
  
function keyReleased() {
  if (keyCode === LEFT_ARROW) {
    character.dir(0, 0);
  } else if (keyCode === UP_ARROW) {
    character.dir(0, 0);
  } else if (keyCode === RIGHT_ARROW) {
    character.dir(0, 0);
  } else if (keyCode === DOWN_ARROW) {
    character.dir(0, 0);
  
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 27, 2019, 10:26am UTC](https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677/2 "2019-03-27T10:26:47Z")

</div>

- @ `var bullet = [];`, you assign a new array to global variable _bullet_.
- However, inside **setup()**, @ `bullet = new Bullet(width/2, height/2);`, you reassign global variable _bullet_ w/ a new Bullet object.
- It means global variable _bullet_ no longer refers to an array, but a single instance of Bullet!

---

<div class="post-metadata">

### Author: ![torridger](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@torridger](https://discourse.processing.org/u/torridger)
#### Post date: [March 27, 2019, 10:38am UTC](https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677/3 "2019-03-27T10:38:35Z")

</div>

Hi, thanks so much for your response. I’m not sure I fully understand though (I’ve just started learning p5.js and programming in general).

I found in the tutorial that i was following that the guy actually disabled that line of code with a comment like: //bullet = new Bullet(width/2, height/2);

I’m still getting the same error however. Any thoughts?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 27, 2019, 11:51am UTC](https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677/4 "2019-03-27T11:51:31Z")

</div>

> [@torridger](#):
>
> I’m still getting the same error however. Any thoughts?

Does it happen when you hit space?

> [@torridger](#):
>
> ```auto
> if (keyCode === 32) {
> var bullet = new Bullet(character.x, height);
> bullet.push(bullet);
> }
> 
> ```

This time, you’re declaring a local variable named _bullet_ within callback **keyPressed()** and assigning to it a new Bullet object.

However, a Bullet object isn’t an array! It’s got no method called **push()** either!

Either way, b/c _bullet_ is a local variable, it ceases to exist when **keyPressed()** returns.

P.S.: I believe I’ve found the source code for that challenge tutorial video:

> <https://github.com/CodingTrain/website-archive/blob/main/CodingChallenges/CC_005_Space_invaders/P5/sketch.js>

---

<div class="post-metadata">

### Author: ![torridger](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@torridger](https://discourse.processing.org/u/torridger)
#### Post date: [March 28, 2019, 4:06am UTC](https://discourse.processing.org/t/p5-js-issue-bullet-push-is-not-a-function/9677/5 "2019-03-28T04:06:22Z")

</div>

Thanks for your help! Managed to resolve it.
