Unity Scripting { get; set; } Error

Helllooooo… I hate life D:

Anyways, hi I am Sonic and I have a problem with my RPG mecanim, I have one script called CharStats, which I can safely assume you know what that stands for… I’m just starting to use get and set for my variables. In my other script called MarioController which controls my character, I want to borrow a variable to check if I’m rolling so it can stop me. [I’m doing this because when I do a roll animation and hold down W it will go faster because my Mario script doesn’t know the difference between rolling and standing up and walking]

Please don’t diss my efficiency, just trying to get a base right now

MarioController:

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

namespace Character
{
    public class MarioController : MonoBehaviour
    {

        CharStats charCheck = new CharStats();

        public CharStats playerCheck;

        public GameObject player;

        

        public float velocity = 5;
        public float turnSpeed = 10;

        Vector2 input;
        float angle;

        Quaternion targetRotation;
        Transform cam;

        Animator anim;


        void Start()
        {
            playerCheck = player.GetComponent<CharStats>();
            cam = Camera.main.transform;
            anim = GetComponent<Animator>();
        }


        private void Update()
        {

            Debug.Log(playerCheck.isRolling);

            GetInput();

            if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;

            CalculateDirection();
            Rotate();
            Move();

        }

        /// <summary>
        /// Input based on Horizontal(a,d,<,>) and Vertical, (w,s,^,v) keys
        /// </summary>

        void GetInput()
        {
            input.x = Input.GetAxis("Horizontal");
            input.y = Input.GetAxis("Vertical");
            anim.SetFloat("BlendX", input.x);
            anim.SetFloat("BlendY", input.y);
        }

        /// <summary>
        /// Direction relative to the camera's rotation
        /// </summary>

        void CalculateDirection()
        {
                angle = Mathf.Atan2(input.x, input.y);
                angle = Mathf.Rad2Deg * angle;
                angle += cam.eulerAngles.y;
        }

        /// <summary>
        /// Rotate toward the calculated angle
        /// </summary>

        void Rotate()
        {
            targetRotation = Quaternion.Euler(0, angle, 0);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
        }

        /// <summary>
        /// This player onlt moves along its own forward axis
        /// </summary>

        void Move()
        {
            transform.position += transform.forward * velocity * Time.deltaTime;
        }
    }
}

CharStats :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityScript;
using UnityEngine.UI;
using UnityEditor.Animations;
using TMPro;
using System;
using Character;

namespace Character
{
    public class CharStats : MonoBehaviour
    {
        [SerializeField]
        private Animator anim;

        public bool isRoll = false;
        public bool isIdle = false;
        public bool isRolling = false;
        public bool isMoving = false;
        public bool isJumping = false;

        public bool IsRolling
        {
            get { return isRolling; }
            set { isRolling = value; }
        }

        #region Stats

        // Name of character
        [Header("_______________________________________")]

        [Header("Character Stats")]

        [Header("_______________________________________")]

        [Header("Name")]
        [SerializeField] private string Name = "Sonic";

        [Header("_______________________________________")]

        // Personal stats
        [Header("Stats")]
        [SerializeField] private int Level = 1;
        [SerializeField] private int Health = 10;
        [SerializeField] private int MaxHealth = 10;
        [SerializeField] private int XP = 0;
        [SerializeField] private int MaxXP = 50;

        [Header("_______________________________________")]

        //Componets for Updating The bars
        [Header("UI")]
        public Slider XPBar;
        public Slider HealthBar;

        [Space]
        public TextMeshProUGUI TextName;

        [Header("_______________________________________")]

        private float ButtonCooler = 0.5f;
        private int ButtonCount = 0;

        #endregion

        void LevelUp()
        {
            Level++;
            XP = 0;
            MaxXP = 50 * Level;
            Health = 35;
            MaxHealth = 35;
        }

        // Use this for initialization
        void Start()
        {

            isRolling = false;

            // Stating the Textname(TextMeshPro Text) 
            // is equal to the name they input
            TextName.text = Name;

            anim = GetComponent<Animator>();

        }

        // Update is called once per frame
        void Update()
        {
            //Every frame it updates the bar
            // It sets the health bar value to current health
            // Sets the max health to the max value of the health bar
            //HealthBar.value = Health;
            //HealthBar.maxValue = MaxHealth;

            //Every frame it updates the bar
            // It sets the XP bar value to current XP
            // Sets the max XP to the max value of the XP bar
            //XPBar.value = XP;
            //XPBar.maxValue = MaxXP;

            if (XP >= MaxXP)
            {
                LevelUp();
            }

            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.Space))
            {
                if (!isRoll)
                {
                    anim.SetBool("isMoving", true);
                    isMoving = true;
                    anim.SetBool("isIdle", false);
                    isIdle = true;
                }
            }
            else
            {
                anim.SetBool("isMoving", false);
                isMoving = false;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {

                if (ButtonCooler > 0 && ButtonCount == 1/*Number of Taps you want Minues One*/)
                {
                    // Has doubled tapped

                    anim.SetBool("DoubleTapped", true);
                    isRoll = true;
                    isRolling = true;
                    anim.SetBool("isRolling", true);
                    Invoke("RollIsDone", 1.7f);

                    Debug.Log("Player Has Doubled Tapped the W key.");
                }
                else
                {
                    ButtonCooler = 0.7f;
                    ButtonCount += 1;
                }
            }

            if (ButtonCooler > 0)
            {
                ButtonCooler -= 1 * Time.deltaTime;
            }
            else
            {
                ButtonCount = 0;
            }

            if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.Space))
            {
                if (!isRolling)
                {
                    anim.SetBool("isIdle", true);
                    isIdle = true;
                }
            }
            else
            {
                anim.SetBool("isIdle", false);
                isIdle = false;
            }

            //print(isIdle);

            //print(isRoll);

        }

        void RollIsDone()
        {
            isRoll = false;
            anim.SetBool("DoubleTapped", false);
            isRolling = false;
            anim.SetBool("isRolling", false);
        }
    }
}

We’re not dissing you about efficiency but about not mentioning what error you actually got. From your comment it looks like you get a NullReference Exception in this line:

Debug.Log(playerCheck.isRolling);

NullReference Exceptions always have the same reason:

You have a reference type variable that you try to dereference but that reference is actually null. If you just look at that line, what potential reference type do you see that you dereference which could be null? Right there is only one: “playerCheck”.

So “playerCheck” is actually null. Why? Well just look where you actually set the reference. In start you have

playerCheck = player.GetComponent<CharStats>();

So that means most likely the gameobject you have referenced in the player variable doesn’t have a CharStats component. Maybe you referenced the wrong gameobject?

Note you made playerCheck public so you could actually assign a value in the inspector. However that would be pointless since you overwrite it in Start.

Your variable has the same name as the function. That is causing the error. [1]

[1]: C# Property Examples - Dot Net Perls here is explained what you did wrong.

Thanks for the response, but I’m still getting an error after I run the game

        public bool isRoll = false;
        public bool isIdle = false;
        bool isRolling = false;
        public bool isMoving = false;
        public bool isJumping = false;

        public bool RollingCurrently
        {
            get { return isRolling; }
            set { isRolling = value; }
        }

Error: NullReferenceException: Object reference not set to an instance of an object
Character.MarioController.Update () (at Assets/Scripts/MarioController.cs:42)