@crazyKnight
I am making a game where the main character (Alva) can shoot bullets and I am using a prefab to make a new bullet every time the player presses “e”, the hit timer is > 100 (this makes it so the player can only swing their sword or shoot every so often) and the bullet range timer is 0 (this timer sets the range for the bullets. when the timer is = 100 the bullet is destroyed). but when I press “E” only about 1 in 10 times does Alva shoot a bullet. This is my bullet script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Bullet : MonoBehaviour

{

public float xSpeed;

public float ySpeed;

GameObject BulletClone;

void Start()
{
    CheckFireDirection();
    GameObject Alva = GameObject.Find("Alva");
    position = this.GetComponent<Transform>();
    transform.position = Alva.transform.position;
}
void Move()
{
    transform.Translate(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, 0);

}

void CheckFireDirection()
{
    GameObject Alva = GameObject.Find("Alva");
    PlayerController alva = Alva.GetComponent<PlayerController>();
    if (this.name != "Bullet_Prefab")
    {
        if (alva.faceUp)
        {
            ySpeed = 15;
            xSpeed = 0;
        }
        else if (alva.faceDown)
        {
            ySpeed = -15;
            xSpeed = 0;
        }
        else
        {
            ySpeed = 0;
        }
        if (alva.faceRight)
        {
            xSpeed = 15;
            ySpeed = 0;
        }
        else if (alva.faceLeft)
        {
            xSpeed = -15;
            ySpeed = 0;
        }
        else
        {
            xSpeed = 0;
        }
    }
}

void CheckRangeTimer()
{
    GameObject Alva = GameObject.Find("Alva");
    PlayerController alva = Alva.GetComponent<PlayerController>();
    if (alva.bulletRangeTimer == 100 && this.name != "Bullet_Prefab")
    {
        Destroy(this.gameObject);
    }
}

void CheckInstantiate()
{
    GameObject Alva = GameObject.Find("Alva");
    PlayerController alva = Alva.GetComponent<PlayerController>();
    GameObject Bullet_Prefab = GameObject.Find("Bullet_Prefab");
    if (Input.GetKeyDown("e") && alva.hitTimer >= 100 && alva.bulletRangeTimer == 0)
    {
        BulletClone = Instantiate(Bullet_Prefab, Alva.transform.position, Quaternion.identity) as GameObject;
         alva.hitTimer = 0;
    }
    
}

   void Update()
{
    CheckInstantiate();
    CheckRangeTimer();
    Move();
}

}

This is where I define the bullet range timer and the hit timer

int bulletRangeTimer;
int hitTimer;

void Move()
{
   if (Input.GetKeyDown("e") && hitTimer >= 100 && bulletRangeTimer == 0)
        {
            hitTimer = 0;
        }
    }

void CheckBulletTimer()

{

    if (Input.GetKeyDown("e") && bulletRangeTimer == 0)

    {

        bulletRangeTimer = 1;

    }

    if (bulletRangeTimer != 0)

    {

        bulletRangeTimer++;

    }

    if (bulletRangeTimer > 100)

    {

        bulletRangeTimer = 0;

    }

}

void Update(){

hitTimer++

CheckBulletTimer();

}

Sorry about so much code. this is just a very broad issue so it is important to know everything about it.

If you can help me in any way that would be greatly appreciated

Michael

The problem appears to be that you are adding to bulletRangeTimer each frame. So unless you happen to press e on that frame, the statement bulletRangeTimer == 0 is going to return false and the code in that if statements block will not run.


On another note, GameObject.Find is not a quick method, do not perform it each frame. Get a reference to the PlayerController like so;

    private PlayerController _alva;

    private void Start()
    {
        _alva = GameObject.Find("Alva").GetComponent<PlayerController>();
        transform.position = _alva.transform.position;
        ///

Then you can do this;

    void CheckRangeTimer()
    {
        if (_alva.bulletRangeTimer == 100 && this.name != "Bullet_Prefab")
        {
            Destroy(this.gameObject);
        }
    }

Although, you could just make it public or a serialized field and populate it in the inspector.

Also, why are you checking if the gameobject the script is on isn’t called Bullet_Prefab?