I've found this wierd bug

When I hit spacebar multiple times immediately after clicking the play button, spaceship keeps firing even if I stop pressing spacebar:-

But if I wait for like one or half second after clicking the play button and then hit the spacebar, everything works fine as it should be:-

Don’t know what I am doing wrong.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
    //Configuration Parameter

    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float padding = 0.4f;
    [SerializeField] float Ypadding = 3.5f;
    [SerializeField] float Health = 200;
    [SerializeField] AudioClip DeathSFX;
    [SerializeField] [Range(0, 1)] float DeathSFXVolume = 0.75f;

    [SerializeField] GameObject LaserPrefab_Player;//To attach "Laser" prefab to "Player" gameobject
    [SerializeField] float ProjectileSpeed_Player = 10f;
    [SerializeField] float offSet_LaserPos_R_x = 0.15f;
    [SerializeField] float offSet_LaserPos_R_y = 0.15f;
    [SerializeField] float offSet_LaserPos_L_x = -0.15f;
    [SerializeField] float offSet_LaserPos_L_y = 0.15f;
    [SerializeField] float ProjectileFiringPeriod_Player =0.1f;
    [SerializeField] AudioClip ProjectileSFX;
    [SerializeField] [Range(0, 1)] float ProjectileSFXVolume = 0.75f;

    float xMin, xMax,yMin,yMax;
    Vector3 LaserPos_R;
    Vector3 LaserPos_L;
    Coroutine FiringCoroutine;

    //Use this for initialisations
    void Start ()

    void Update ()
    private void Fire()
        LaserPos_R = transform.position;
        LaserPos_R.x += offSet_LaserPos_R_x;
        LaserPos_R.y += offSet_LaserPos_R_y;

        LaserPos_L = transform.position;
        LaserPos_L.x += offSet_LaserPos_L_x;
        LaserPos_L.y += offSet_LaserPos_L_y;

        if (Input.GetButtonDown("Fire1"))
        else if (Input.GetButtonUp("Fire1"))

    IEnumerator FireContinuously()
        while (true)
            GameObject laser_R = Instantiate(LaserPrefab_Player, LaserPos_R, Quaternion.identity) as GameObject;
            laser_R.GetComponent<Rigidbody2D>().velocity = new Vector2(0, ProjectileSpeed_Player);

            GameObject laser_L = Instantiate(LaserPrefab_Player, LaserPos_L, Quaternion.identity) as GameObject;
            laser_L.GetComponent<Rigidbody2D>().velocity = new Vector2(0, ProjectileSpeed_Player);

            AudioSource.PlayClipAtPoint(ProjectileSFX, Camera.main.transform.position, ProjectileSFXVolume);

            yield return new WaitForSeconds(ProjectileFiringPeriod_Player);

    private void OnTriggerEnter2D(Collider2D other)
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if (!damageDealer) { return; }

    private void ProcessHit(DamageDealer damageDealer)
        Health -= damageDealer.GetDamage();
        if (Health <= 0)

    private void Die()
        AudioSource.PlayClipAtPoint(DeathSFX, Camera.main.transform.position, DeathSFXVolume);

    private void Move()
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;//Change what player is going to do in y axis
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;//Change what player is going to do in X axis

        var newYpos = Mathf.Clamp(transform.position.y + deltaY,yMin,yMax);//Position that we want ship to go in y axis
        var newXPos = Mathf.Clamp(transform.position.x + deltaX,xMin,xMax);//Position that we want ship to go in x axis

        transform.position = new Vector2(newXPos, newYpos);//will make ship position change

    private void SetUpMoveBoundaries()
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - Ypadding;

Hi Ankush,

I doubt that you are not doing anything wrong. Add a Debug.Log in each if/else if block in your Fire method to see when and how many times they get executed.

Hi Nina.

Ok, So I did what you said:

if (Input.GetButtonDown("Fire1"))
            Debug.Log("Firing Coroutine");
        else if (Input.GetButtonUp("Fire1"))
            Debug.Log("Stop Coroutine");

& played it 2,3 times
Output 1
Output 2
Unity should be calling Stop Coroutine immediately after Firing Coroutine but it doesn’t. Is it a unity bug or something else because it works fine if I wait…

Is it a unity bug or something else because it works fine if I wait…

I don’t know any student who had this particular issue. If you think this could be a bug, try to update Unity.

Maybe you could also check which keys and buttons are connected with “Fire1” in the Input Manager. And if there are some other input devices plugged into your PC.

Any update on this??

Sorry, for the late reply my college exams were going on. I tried re-installing unity and it worked

1 Like

No problem, Exams are more important! Glad you found a solution and thanks for getting back to us :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms