Having trouble with variables not updating on a script derived from another script

I am currently using this script…

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

[RequireComponent(typeof(CharacterStats))]
public class CharacterControllerIndividual : CharacterStats
{
    public GameObject MyCharacterName;

   
    public float lookRadius = 10f;
   
    public PlayerMovement pm;

    Transform target;
    NavMeshAgent agent;
    CharacterCombat combat;
    PlayerManager playerManager;

   


    void Awake()
    {

        if (MyCharacterName.name == "Miria")
        {
            Debug.Log("Yes");
            //individual character stat initializations
            lookRadius = 10f;

           
            agent = GetComponent<NavMeshAgent>();
            combat = GetComponent<CharacterCombat>();

           
           

           

        }
    }

    void Start()
    {


       
        target = PlayerManager.instance.player.transform; //set up to target only the player [Brackeys ep10] i feel you need to set this different for multiple enemies
       
       

       
        SetLevelNumber(CharacterLevels[0]);
        level = CharacterLevels[0];

        typeOfCharacter = 3;
        maxHealth = EnemyHPList[level - 1];
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);
        YokiAuraRange = 3;
    }


    void Update()
    {
        agent.speed = 2 * ((100 / (pm.publicMoveSpeedPercentage / 100)) / 100);


        float distance = Vector3.Distance(target.position, transform.position); //tracks the player only
        print(maxYoki);
        level = CharacterLevels[0];
    }

    void FaceTarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    }
}

and when i play the game the first time the variables are right … for instance maxYoki = 100 off of charactercontrollerindividual, but when i change maxYoki on CharacterStats…

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


[RequireComponent(typeof(EnemyStats))]

public class CharacterStats : MonoBehaviour
{
    //public PlayerMovement playerMovement;


    public YokiBar yokibar;
    public HealthBar healthBar;
    public Text levelText;

    public Stat damage;
    public Stat armor;
    public int level;
    public int can;


    List<int> PlayerDamageList = new List<int>() { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5 };

    List<int> EnemyDamageList = new List<int>() { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6 };

    public List<int> PlayerHPList = new List<int>() { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 };

    public List<int> EnemyHPList = new List<int>() { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13 };

    public List<int> YokiPowerList = new List<int>() { 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 };

    //All different stats that allign with different character class stats
    List<string> CharacterMMOStats = new List<string>() { "1", "1",    "1",      "1",     "1",       "1",    "Offensive"  };
    // Stat Discription(1000=max) ----------------------------- Yoki,Agility,Strength,Spirit,Perception,Leadership, Type,     
    //1-99 = E, 100-199 = E+, 200-299= D, 300-399 = D+, 400-499 = C, 500-599 = C+, 600-699 = B, 700-799 = B+, 800-899 = A, 900-999 = A+, 1000 = S


    string[,,] MyCharacterClasses = new string[2,2,9]{
        {
            {"Claymore","Miria","B+", "A+", "C", "B", "C", "A+", "Offensive"}, //Claymore
            {"Claymore","Clare", "E", "E", "E", "D", "C+", "E", "Offensive"}
        },{
            {"Claymore","Miria","B+", "A+", "C", "B", "C", "A+", "Offensive"}, // Some Other Series Characters
            {"Claymore","Clare", "E", "E", "E", "D", "C+", "E", "Offensive"}
        }
    };




    //List of Characters, what abilities they can use, and what their ability levels are
    string[,] CharacterAbilitiesRank = new string[ 2, 5]{
        { "Player", "Mirage", "3", "YokiSuppression", "10" },
        { "Miria", "Mirage", "1", "YokiSuppression", "10" }
    };

    //CharacterLevels --
    public List<int> CharacterLevels = new List<int>()
    {
        6 //Miria
    };


    //Character levels above
    //Character Abilities
    void Start()
    {   //begining array -- Possible Character Abilities -- For each individual character
        string[][,] MiriaCA = new string[2][,];

        //Individual arrays for different characters
        MiriaCA[0] = new string[,] { { "Mirage", "1" } , { "Mirage", "2" } , { "Mirage", "3" } };
        MiriaCA[1] = new string[,] { { "YokiSuppression", "1" }, { "YokiSuppression", "2" }, { "YokiSuppression", "3" }, { "YokiSuppression", "4" }, { "YokiSuppression", "5" },
            { "YokiSuppression", "6" }, { "YokiSuppression", "7" }, { "YokiSuppression", "8" }, { "YokiSuppression", "9" }, { "YokiSuppression", "10" } };

       
      

    }













    public int typeOfCharacter = 0;
    public int maxHealth = 100; //change to type Stat to add modifiers
    public int currentHealth { get; set; } //any class can get it but only this class can change it


    public int maxYoki = 101;
    public int currentYoki { get; set; }
    public int YokiPercent = 0;
    public float YokiAuraRange = 5f;


    //yokibar.SetMaxYoki(maxYoki);
    //yokibar.SetYoki(30);
    //yokibar.SetYokiPowerNumber(currentYoki);
    // currentYoki = 20;
    //maxYoki = 50;

    public GameObject Player;

    [SerializeField] private LevelWindow levelWindow;

    void Awake()
    {
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);

        currentYoki = maxYoki;
        yokibar.SetMaxYoki(maxYoki);

        EventManager.current.onMirageStarted += MirageStarted;
        EventManager.current.onMirageEnded += MirageEnded;

        YokiPercent = 0;
        yokibar.SetYokiPowerNumber(YokiPercent);
    }

    void OnDrawGizmosSelected() //YokiSuppression
    {
       
        Gizmos.color = Color.red;

        if (typeOfCharacter == 1f)
            Gizmos.DrawWireSphere(transform.position, (float)Convert.ToInt32(CharacterAbilitiesRank[0, 4]));
        else
            Gizmos.DrawWireSphere(transform.position, YokiAuraRange);
    }

    private void MirageStarted() // the actual event... type what you want to happed
    {
        if (typeOfCharacter == 1f) //since all entities call this, it only activates if the character id is 1, or the player ^.^
        {
            currentYoki -= 5; //changes yokibar by -5 when using mirage
            yokibar.SetYoki(currentYoki);
            YokiPercent = 60;
            yokibar.SetYokiPowerNumber(YokiPercent);


            PlayerMovement pm = Player.GetComponent<PlayerMovement>();


            if (CharacterAbilitiesRank[0, 2] == "1")
            {
                pm.publicMoveSpeedPercentage = 3355.4f;
                pm.playerCameraTurningSpeedPercentage = .1f;
            }
            else if (CharacterAbilitiesRank[0, 2] == "2")
            {
                pm.publicMoveSpeedPercentage = 2500f;
                pm.playerCameraTurningSpeedPercentage = .4f;
            }
            else if (CharacterAbilitiesRank[0, 2] == "3")
            {
                pm.publicMoveSpeedPercentage = 5000f;
                pm.playerCameraTurningSpeedPercentage = 0f;
            }
        }

    }

    private void MirageEnded()
    {
       
        PlayerMovement pm = Player.GetComponent<PlayerMovement>();
        YokiPercent = 0;
        yokibar.SetYokiPowerNumber(YokiPercent);
        pm.publicMoveSpeedPercentage = 100f;
        pm.playerCameraTurningSpeedPercentage = 1f;
       
    }

    public void SetLevelNumber(int levelNumber)
    {
        levelText.text = "" + levelNumber;

    }

    public void TakeDamage(int damage, int aL)
    {


        if (typeOfCharacter == 1f) //player taking damage
        {
           
            damage += EnemyDamageList[aL - 1];
           
        } else if (typeOfCharacter == 2f)
        {
            damage += PlayerDamageList[levelWindow.level - 1];
           

        }
        damage -= armor.GetValue(); //armor right now negates damage by raw #

        damage = Mathf.Clamp(damage, 0, int.MaxValue); //so if armor modifier is greater than damage it doesn't heal you, but you take 0 dmg

        print(damage);
        damage = (int)Math.Ceiling((((((int)(Math.Ceiling((float)Convert.ToInt32(CharacterMMOStats[2]) / 100)))+1) * damage * ((float)YokiPercent/100)) + damage)); //damage plus yoki%used * damage * mmo lvl

        print(damage);
       
       
        currentHealth -= damage;
        Debug.Log(transform.name + " takes " + damage + " damage.");

        healthBar.SetHealth(currentHealth);

        if (currentHealth <= 0)
        {
            Die();
        }
    }

   


    public virtual void Die()
    {
        Debug.Log(transform.name + "died.");

        Destroy(gameObject);

        levelWindow.AddExperience(level);
        levelWindow.AddCoins((int)level);
    }
}

line 100 too lets say, 110, when i do the print(maxYoki) function from the first script, it still says 100, instead of 110. I have charactercontrollerindividual deriving off of characterstats, so why will it not update the variable when i change it in characterstats? Thanks!

Usually because it is serialized in the inspector. You can either changed there or put a [NonSerialized] attribute over the variable (so it will be hidden in the inspector too and only your code will initialize the variable). And in case you’re doing what your code is implying, you can’t change a variable like that, only in the declaration line public int maxYoki = 101; or in a method.

1 Like

Okay, so it works, but technically I had to use [System.NonSerialized], denoted here Unity - Scripting API: NonSerialized. Thank you so much!

Also, since I am new, is there any way to close this form, or do I just leave it as is.

You already have the using System; statement.

Edit: sorry, that’s in the other file.

You mean your thread? Leave it as it is.

1 Like