My class "Playerbehavior" is not inheriting the other class "Characterbase"

Asked

Viewed 45 times

1

I’m not getting access to the class CharacterBase of my Script PlayerBehavior

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

public enum TypeCharacter{
    Priest = 0,
    Mage = 1,
    Paladin = 2,
    Warrior = 3,
    Archer = 4,
    Newbie = 5

}

public enum PlayerStates
{

    Movement

}



public class PlayerBehavior : CharacterBase
{


    private TypeCharacter type;

    private AnimationController animationController;

    /* State Machine*/
    private PlayerStates currentState = PlayerStates.Movement;
    //


    //*Movimentation*//
    private float speed;
    public float speedRun;
    public float runStaminaCost;
    public float speedDodge;
    public float dodgeStaminaCost;
    public float timeRecoverDodge;
    private float currentTimeStaminaRecoverDodge;
    public float speedWalk;
    public float rotateSpeed;
    private float horizontal;
    private float vertical;
    private CharacterController controller;
    private float currentStamina;
    private float maxStamina;
    public float staminaRecover;





    //*Attack*//

    private int currentAttack = 0;
    public float attackRate;
    private float currentAttackRate;
    private float rangeAttack;
    public int totalAttackAnimations;

    //*      *//

    //UI
    public UIController UI;



    new protected void Start () {

        base.Start ();

        PlayerStatsController.SetTypeCharacter (TypeCharacter.Newbie);
        currentLevel = PlayerStatsController.GetCurrentLevel();
        type = PlayerStatsController.GetTypeCharacter();


        basicStats = PlayerStatsController.instance.GetBasicStats(type);

        animationController = GetComponent<AnimationController>();
        speed = speedWalk;

        controller = GetComponent<CharacterController> ();

        currentAttack = basicStats.baseAttack;

        base.Start();


        maxStamina = basicStats.baseStamina * basicStats.agillity;

        currentStamina = maxStamina;


    }

    new void Update()
    {
        base.Update();

        if (Input.GetKeyDown(KeyCode.T))

        {
            currentLife -= Random.Range(1, 30);
        }

        UI.SetLife(basicStats.startLife, currentLife);


    }

    void Update ()  {

        switch (currentState) {
        case PlayerStates.Movement:

            vertical = Input.GetAxis ("Vertical");
            horizontal = Input.GetAxis ("Horizontal");

            if(Input.GetKey (KeyCode.LeftShift) && vertical != 0) {speed = speedRun; 
                animationController.PlayAnimation (AnimationStates.RUN);}

            else { speed = speedWalk;
                if (vertical != 0)
                    animationController.PlayAnimation (AnimationStates.WALK);}



            controller = GetComponent<CharacterController>();
            transform.Rotate(0, horizontal * rotateSpeed, 0);
            Vector3 forward = transform.TransformDirection(Vector3.forward);
            float curSpeed = speed * vertical;
            controller.SimpleMove(forward * curSpeed);

            /*Attack */ 
            if (Input.GetButtonDown("Fire1"))
            {
                Attack (); }

            currentAttackRate += Time.deltaTime;
            break;


        }


}

    private void Attack()
        {

        if (currentAttackRate >= attackRate) {
            currentAttackRate = 0;
            animationController.CallAttackAnimation (currentAttack);
            currentAttack++;

            if (currentAttack > totalAttackAnimations) {
                currentAttack = 0;
            }
        }

            Ray rayAttack = new Ray (transform.position, transform.forward);

            RaycastHit hitinfo = new RaycastHit ();

            rangeAttack = basicStats.baseRange;

            if (Physics.Raycast (rayAttack, out hitinfo, rangeAttack)) {
                if (hitinfo.collider.GetComponent<DestructiveBase> () != null) {
                    if (hitinfo.collider != GetComponent<Collider> ()) {
                        hitinfo.collider.GetComponent<DestructiveBase> ().ApplyDamage (currentAttack);
                    }
                }
            }
        }
    }


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

[System.Serializable]
//Basics Atribuits//
public class BasicStats{
public BasicStats baseInfo;
public float startLife;
public float startMana;
public int strenght;          /*STR*/
public int intelligence;      /*INT*/
public int agillity;          /*DEX*/
public int concentration;     /*CON*/
public int baseDefense;
public int baseAttack;
public float baseRange;
public float baseStamina;
}

public abstract class CharacterBase : DestructiveBase
{

public int currentLevel;
public BasicStats basicStats;

// Use this for initialization
protected void Start () {

    currentLife = basicStats.startLife;


}
// Update is called once per frame
protected void Update()
{

}

}

  • And where this class CharacterBase is defined?

  • What do you mean ? I’m new to programming even if I have a little patience thank you.

  • You made the class PlayerBehavior inherit the class CharacterBase, but this second is not defined in the code. You can put its implementation in the question and explain better what you wanted with "I’m not getting access to the class"?

  • can understand like this ?

  • Unity is accusing that the error is here basicStats = Playerstatscontroller.instance.Getbasicstats(type); so it means that the script is not inheriting the base Character class to get the correct basicstats ?

  • which error is giving ?

  • Nullreferenceexception: Object Reference not set to an instance of an Object

  • PlayerStatsController or PlayerStatsController.instance or type is null only, nothing to do with inheritance

  • and how do I fix it ?

  • instantiating the object that is null =]

  • And how do I instate a null object ? sorry I’m new to programming this script is tutorial and I’m modifying it my way.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.