Function not being called via interface (Solved)

I have a player object carrying multiple guns. The guns each have their own fire delay, but they also pass a global fire delay to the player to limit it in firing multiple guns. The guns all derive from GunBase, which is where the relevant code is. The player is a MonoBehavior, and also implements the contract ifcGunUser.

The odd thing is the code that calls the interface method is being called, with the correct interface reference, but the interface method never gets called. (All checked with the log.) Can anyone help me learn what I am doing wrong?

The code is trimmed to what I think is relevant for readability; I’ll add more if needed.

PlayerController:

public class PlayerController : MonoBehaviour, ifcGunUser {
    private float nextFire = 0;

    public void GunStun(float time)
    {
        nextFire = Mathf.Max(nextFire, (Time.time + time));
        Debug.Log(Time.time + " GunStun, nextFire = " + nextFire);  //Never shows up in the log
    }
}

ifcGunUser:

public interface ifcGunUser
{
    void GunStun(float time);
}

GunBase:

public class GunBase : MonoBehaviour {
    public ifcGunUser owner;
    public Transform firePosition;
    public GameObject projectile;

    public float fireStun;  //Time after firing before ANY weapon can be fired

    public virtual void DoShot()
    {
        Instantiate(projectile, firePosition.position, firePosition.rotation);
        Debug.Log ("GunBase, calling GunKickback: " + fireStun); //This shows up with the correct value
        Debug.Log ("Interface: " + owner);  //This shows up with Interface: soldier (PlayerController)
        owner.GunKickback(fireStun);
    }
}

Don’t know if this is a typo while you were trimming down the code for the post, but your interface defines a method called “GunStun”, but your GunBase class is calling “GunKickback”.

Well, that’s an embarrassing error. Yes and no. I was calling the wrong interface method. GunKickback is a stub that will do something completely different. Thanks for your help, and good job catching it even though I trimmed something I shouldn’t have.

1 Like

No worries. :slight_smile: Happens all the time, especially when you have code completion and multiple methods with similar names.