Home > Mobile >  Variable not being set in class using inheritance
Variable not being set in class using inheritance

Time:10-03

Edit: It turns out it was how I was getting the ProjectileWeapon component. I was getting the one that was on the non-instantiated prefab instead of getting the one on weapon gameobject. I changed it so that the code instantiates the game object first (or gets the existing one if we've already picked it up), and then get the component from that. So the rest of the code works fine. Now I can move on and improve it!

I have an issue with the class below called ProjectileWeapon. It is based on an abstract class called Weapon, and that class inherits MonoBehaviour.

Weapon has two abstract functions called BeginCycle and EndCycle which are implemented in the ProjectileWeapon class. Those functions set a variable called "firing".

The problem is, "firing" doesn't ever seem to be set despite the functions being called correctly. I know the functions are called because I can see the prints in the console.

Also, when I use that variable in the update function, it doesn't do anything because the variable never changes.

The OnGUI function is working and is displaying text on screen, however the "firing" variable is never updated.

Am I misunderstanding how to use inheritance?

This class is on the weapon prefab which is then instantiated during the equipping function in the game

public class ProjectileWeapon : Weapon
{

    private bool firing;
    private float firingTimer;

    void Start()
    {
        print("ProjectileWeapon start");
    }
    void OnGUI()
    {
        GUI.Label(new Rect(0,100,100,100), "ProjectileWeapon firing: "   firing);
    }

    void Update()
    {
        // this function is called but "firing" is not updated
    }

    public override void BeginCycle()
    {
        print("projectile begin cycle");
        firing = true;
    }

    public override void EndCycle()
    {
        print("projectile end cycle");
        firing = false;
    }
}

Here's the base class:

public abstract class Weapon : MonoBehaviour
{
    public abstract void BeginCycle();
    public abstract void EndCycle();
}

EDIT: Here is the code that calls the above This component is added to the player game object

public class WeaponHandler : MonoBehaviour
{
    public bool FireInput { get; set; } // set to true when user holds the mouse button down, and false when let go

    public Weapon WeaponBehaviour; // this is the script that does the weapon functionalility. Any subclass of Weapon can be put here e.g. ProjectileWeapon, MeleeWeapon

    private bool isFiring = false;

    void OnGUI()
    {
        GUI.Label(new Rect(0,0,100,100), "fire input:"   FireInput   ", isFiring:"   isFiring);
    }

    void Update()
    {
        // fire weapon
        if (WeaponBehaviour && FireInput && !isFiring)
        {
            ActivateWeapon();
        } 
        else if (!FireInput && isFiring)
        {
            DeActivateWeapon();
        }
    }

    private void ActivateWeapon()
    {
        print("activate weapon");
        isFiring = true;
        WeaponBehaviour.BeginCycle();
    }

    private void DeActivateWeapon()
    {
        print("deactivate weapon");
        isFiring = false;
        WeaponBehaviour.EndCycle();
    }
}
 

CodePudding user response:

Based on the code you've provided, there are three possible scenarios:

  1. You're not calling the functions. However, if the print functions are called, then you must be calling them.

  2. You're calling both functions, which sets the variable to true, and then to false.

  3. You're overriding the variable with a local variable with the same name. Visual Studio will warn you if that's the case.

It's hard to tell without knowing where the variables are supposed to be called. If you upload the rest of the code, I'm sure the answer will be clear.

CodePudding user response:

It turns out it was how I was getting the ProjectileWeapon component. I was getting the one that was on the non-instantiated prefab instead of getting the one on weapon gameobject. I changed it so that the code instantiates the game object first (or gets the existing one if we've already picked it up), and then get the component from that. So the rest of the code works fine. Now I can move on and improve it!

  • Related