Why is my bullet script not working?

I cannot find any problem with my bullet script. I add this script to my gun but the bullet won’t spawn.

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

public class Shooting : MonoBehaviour {

    //Identify components
    public Rigidbody2D rb;
    public Transform tf;

    //Identify game objects
    public GameObject bullet;

    public bool isShooting = false;

    public float bulletShootingSpeed = 1.0f;

    void Start() { }

    void Update() {
        canShoot();
    }

    IEnumerator BulletShootingSpeed()
    {
        spawnBullet();
        yield return new WaitForSeconds(bulletShootingSpeed);
    }

    void spawnBullet()
    {
        while (isShooting == true)
        {
            Instantiate(bullet, tf.position, Quaternion.Euler(new Vector2(0, 0)));
        }
        
    }

    void canShoot()
    {
        while (Input.GetKey(KeyCode.F))
        {
            isShooting = true;
        }
    }
}

Good day.

Next time check your post… it was duplicated… had to edit it to prevent users to lose theiir time…

Second, you need to learn some C basics… you are using “while loops” and as i can see from this code you don’t understand what they do… so please. Go learn some code.

And isShooting never become false… what you expected?

Worst code ever readed here.

Unity answers is for help with Unity functions and propetrties, not to learn to code.

Bye.

What a terrible code omg :smiley:

Please change all code, try another method of shooting. But in your code you never calling shooting Coroutine. Just change CanShoot method to this:


    void canShoot()
    {
        while (Input.GetKey(KeyCode.F))
        {
            isShooting = true;
            StartCoroutine(BulletShootingSpeed());
        }
    }