Hello,
I’m currently programming a weapon system with automatic, semi auto, melee and throwable weapons.
It is built up using inheritance (see UML diagram).
I made a overriden Update loop that increases the fireRateCounter in the AutomaticWeapon class.
But when I call Fire() from the Player it doesn’t work. But when I call it from AutomaticWeapon’s Update or Start it works.

I really dont understand that problem, a solution would be awsome.
Thanks you.



using UnityEngine;

public abstract class Weapon : MonoBehaviour {

    public abstract void Fire();

    public abstract void Update();
}


public class AutomaticWeapon : Weapon {

    public float fireRate = .2f;
    public float fireRateCounter;

    public override void Fire() {
        Debug.Log(fireRateCounter); //INCORRECT OUTPUT; Why is it 0???

        if (fireRateCounter >= fireRate) {
            Debug.Log("FIRE()");
        }
    }

    public override void Update() {
        fireRateCounter += Time.deltaTime;

        Debug.Log(fireRateCounter); //CORRECT OUTPUT; Why is it correct here?
    }
}

using UnityEngine;

public class Player : MonoBehaviour {

    public Weapon weapon;

    private void Update() {
        if (Input.GetMouseButton(0)) {
            weapon.Fire();
        } 
    }
}

I can’t see any error in the provided code. I would simply assume that this “weapon” variable does not point to the correct object in the scene but rather to a prefab in the project. This is a common mistake and can easily be tested by adding a context object to the Debug.Log statements as second parameter. This way the Unity editor will “ping” / highlight this object whenever you click on the log message in the console. This makes it easier to figure out what object did actually produce this log message.

Also you should always use meaningful and unique log messages so you do not confuse them.

// use this in Update
Debug.Log("AutomaticWeapon::Update current fire rate counter = " + fireRateCounter, gameObject);

// use this in Fire
Debug.Log("AutomaticWeapon::Fire current fire rate counter = " + fireRateCounter, gameObject);