Update Stats During Game

Hi guys, i would like to ask if someone can handle me here… I hv a player script that handles is base stats, health and so on, i hv a inventory system that pick up items that can be equiped and change stats, but wt i would like to know is how i can do things like this:

i have my maxHealth = 5000 for example, and i want to every time i equipe a item that give me stamina, my maxhealth wil increase like maxHealth + 0.5 (my stamina), same doe damage ( base damage + 0.8(strenght))

the problem is if i use update method it grows over and over again, the other problem is if i use in calstats i pick item i equip it calculates but if i take it from character windows to inventory it canculates again…

if someone can help i appreciate… heres scripts:

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

public class Player : HumanoidControl
{
    private static Player instance;
   
    public static Player Instance
    {
        get
        {
            if(instance == null)
            {
                instance = GameObject.FindObjectOfType<Player>();
            }
            return Player.instance;
        }
    }
   
    public Image healthBar;
   
    public Text healthtext;
   
    public static Transform opponent;
   
    public static Player player;
   
    bool block;
   
    private Animator anim;
   
    private bool attacking;

    private bool running;

    private bool sendDamage;

   
    //character base status
    public int baseIntellect;
    public int baseAgility;
    public int baseStrength;
    public int baseStamina;
   
    //character modified status with equipment
    private int intellect;
    private int agility;
    private int strength;
    private int stamina;
   
    public Text statsText;


    public GameObject fireballPrebab;



    void Awake()
    {
        player = this;
       
        anim = GetComponent<Animator>();
    }
   


    void Start()
    {
        curHealth = maxHealth;
               
        SetStatus (0, 0, 0, 0);
    }


   
    void Update()
    {
        healthBar.fillAmount = curHealth / maxHealth;
       
        healthtext.text = "Health: " + curHealth + "/" + maxHealth;
       
        Attack();

        AnimatorStateInfo animStateInfo = anim.GetCurrentAnimatorStateInfo(0);

        if (animStateInfo.IsName("Attack") && !sendDamage && animStateInfo.normalizedTime > 0.36f)
        {
            //sendDamage = false;

            //opponent.GetComponent<Enemy>().GetHit(damage);

            float Shot = anim.GetFloat("Shot");

            if(!sendDamage && Shot > 0.36f)
                opponent.GetComponent<Enemy>().GetHit(damage);
        }

        if (Input.GetKeyDown (KeyCode.Alpha1))
        {
            Vector3 pos = Input.mousePosition;

            GameObject go = Instantiate(fireballPrebab, transform.position, transform.rotation) as GameObject;

            go.transform.position = Vector3.Lerp (transform.position, pos, 10 * Time.deltaTime);
        }
    }


   
    public void SetStatus(int intellect, int agility, int strength, int stamina)
    {
        this.intellect = intellect + baseIntellect;
        this.agility = agility + baseAgility;
        this.strength = strength + baseStrength;
        this.stamina = stamina + baseStamina;

        statsText.text = string.Format ("Intellect: {0}\nAgility: {1}\nStrength: {2}\nStamina: {3}", this.intellect, this.agility, this.strength, this.stamina);
    }
   
   
   
    void OnTriggerStay(Collider other)
    {
        if (other.name == "HealthRegen")
        {
            curHealth += 10;
        }
        if (curHealth > maxHealth)
        {
            curHealth = maxHealth;
        }
    }
   
   
   
    protected override void Attack()
    {
        if (Input.GetMouseButtonUp (1))
        {
            if(opponent != null && Vector3.Distance(opponent.position, transform.position) < attackRange)
            {
                if(!block)
                {
                    sendDamage = true;

                    transform.LookAt(opponent);

                    opponent.GetComponent<Enemy>().GetHit(damage);
                       
                    attacking = true;

                    anim.SetTrigger("IsAttacking");

                    block = true;

                    Invoke("UnBlock", attackSpeed);
                }

                if(Vector3.Distance(opponent.position, transform.position) < attackRange && opponent != null)
                {
                    attacking = true;
                }
                else
                {
                    attacking = false;
                }
                anim.SetBool ("IsAttacking", attacking);
            }
        }
    }
   
   
   
    void UnBlock()
    {
        block = false;
    }
}
using UnityEngine;
using System.Collections;
using System;

public class CharacterPanel : Inventory
{
    public Slot[] equipmentSlots;

    private static CharacterPanel instance;

    public static CharacterPanel Instance
    {
        get
        {
            if(instance == null)
            {
                instance = GameObject.FindObjectOfType<CharacterPanel>();
            }

            return CharacterPanel.instance;
        }
    }

    public Slot WeaponSlot
    {
        get{ return equipmentSlots[9]; }
    }

    public Slot OffHandSlot
    {
        get{ return equipmentSlots[10]; }
    }

    void Awake()
    {
        equipmentSlots = transform.GetComponentsInChildren<Slot>();
    }



    public override void CreateLayout()
    {

    }



    public void EquipItem(Slot slot, ItemScript item)
    {
        if (item.Item.ItemType == ItemType.MAINHAND || item.Item.ItemType == ItemType.TWOHAND && OffHandSlot.IsEmpty)
        {
            Slot.SwapItems(slot, WeaponSlot);
        }
        else
        {
            Slot.SwapItems (slot, Array.Find (equipmentSlots, x => x.canContain == item.Item.ItemType));
        }
    }



    public override void ShowToolTip(GameObject slot)
    {
        Slot tmpslot = slot.GetComponent<Slot>();
       
        if (slot.GetComponentInParent<Inventory>().IsOpen && !tmpslot.IsEmpty && InventoryManager.Instance.HoverObject == null)
        {
            InventoryManager.Instance.visualTextObject.text = tmpslot.CurrentItem.GetToolTip();
           
            InventoryManager.Instance.sizeTextObject.text = InventoryManager.Instance.visualTextObject.text;
           
           
            InventoryManager.Instance.tooltipObject.SetActive(true);
               
            InventoryManager.Instance.tooltipObject.transform.position = slot.transform.position;
        }

    }



    public void CalcStats()
    {
        int intellect = 0;
        int agility = 0;
        int strength = 0;
        int stamina = 0;

        foreach (Slot slot in equipmentSlots)
        {
            if(!slot.IsEmpty)
            {
                Equipment e = (Equipment)slot.CurrentItem.Item;

                intellect += e.Intellect;
                agility += e.Agility;
                strength += e.Strength;
                stamina += e.Stamina;
            }
        }

        Player.Instance.SetStatus (intellect, agility, strength, stamina);
    }



    public override void SaveInventory()
    {
        string content = string.Empty;

        for (int i = 0; i < equipmentSlots.Length; i++)
        {
            if(!equipmentSlots[i].IsEmpty)
            {
                content += i + "-" + equipmentSlots[i].Items.Peek().Item.ItemName + ";";


            }
        }

        PlayerPrefs.SetString ("CharPanel", content);

        PlayerPrefs.Save ();
    }



    public override void LoadInventory()
    {
        foreach (Slot slot in equipmentSlots)
        {
            slot.ClearSlot();
        }

        string content = PlayerPrefs.GetString ("CharPanel");

        string[] splitContent = content.Split (';');

        for(int i = 0; i < splitContent.Length - 1; i++)
        {
            string[] splitValues = splitContent[i].Split('-');

            int index = Int32.Parse(splitValues[0]);

            string itemName = splitValues[1];

            GameObject loadedItem = Instantiate(InventoryManager.Instance.itemObject);

            loadedItem.AddComponent<ItemScript>();

            if(index == 9 || index == 10)
            {
                loadedItem.GetComponent<ItemScript>().Item = InventoryManager.Instance.ItemCountainer.Weapons.Find(x => x.ItemName == itemName);
            }
            else
            {
                loadedItem.GetComponent<ItemScript>().Item = InventoryManager.Instance.ItemCountainer.Equipment.Find(x => x.ItemName == itemName);

            }

            equipmentSlots [index].AddItem(loadedItem.GetComponent<ItemScript>());

            Destroy(loadedItem);

            CalcStats();
        }
    }
}
using UnityEngine;
using System.Collections;

public abstract class HumanoidControl : MonoBehaviour
{
    public string characterName;

    public float attackRange;

    public int damage;

    public float curHealth;

    public float maxHealth;

    public float attackSpeed;



    void Awake()
    {
        curHealth = maxHealth;
    }

    void Start()
    {

    }

    public void GetHit(int playerDamage)
    {
        curHealth = curHealth - playerDamage;
       
        if (curHealth <= 0)
        {
            Destroy (gameObject, 2f);
        }
    }

    protected abstract void Attack();
}

PS: the humanoid script handle variables that can be used both by player and enemy, like health and so on.

You need to introduce the notion of the human-editable variables being called “Base Ability” for example, one for each attribute your’e tracking (health, attack, damage, etc.). Those values are NOT modified by what you equip or buffs/debuffs that are on you.

Next you make a property or method that gives you “Effective Ability” and takes into account your present actual health (based off of Base), and applies modifiers to give the resultant value right now, based on any number of things that are going on.

For extra credit, you can make your get effective ability method also tell you if the effective ability is above, below, or the same as the original ability, and then your UI can use that to display the stats perhaps in color (white, red, green), or else with a little (+x) indicator next to it, or up/down arrow to let the player know he is off-base.

That way no matter how many times you add/remove/equip/unequip stuff, the base values stay where they were.

the problem is that i massed up everything when i start my inventory, and then the move and stats for items in inventory system and after all i think im complelty lost in my proper work looool

An important part of engineering is knowing when to refactor (rewrite) and how to do it in the least-disruptive way. I like to tackle one single subsystem at a time, making sure things continue to work, and I commit my work every time I get a milestone out of the way.

Oh yes, speaking of commit: you (and EVERYBODY!) should also be using version control so that if you rewrite things and go down a blind alley or completely mess everything up, you can easily restore your code and project to a “known good” state. It’s never too late to download git and start using it right this very second. There are plenty of tutorials, especially with how to set it up for Unity properly, which requires two little changes in the Editor Settings area to do it properly.

i will take a look… but i really need 1st of all learn code properly… since almost everything i do is following some tutorials and try things ou, its still hard for me to code something by myself from scratch… and i hv a bad habbit i dont like small things and easy, cuz i start working right of the bat in a rpg type thing xD and i really cant find a good source of learning for c# in unity orientation…

because for example if i follow all script videos in unity learn section i still cant create a database for items or player stats allocation, or even create a quest system, or a skill system… -.- seems nothing has to be with the simple things we learn in the videos…

that is even MORE of a reason to use source control now because if you get something working, it will be INCREDIBLY frustrating if you break it by subsequent tinkering, and then are unable to un-break it.

learning new things is all about tinkering and trying things out and not all of those things will work and some might be very devastating and sweeping if they don’t end up working out.

yap… and almost all tutorials/series/ are for 3rd person games or 1st person shooters, when things go for click to move diablo type game everything changes and the information is almost nothing in that concerns… like simple things like shot a projectile from player to mouse position in the world…

Unity 3 and 4 had that Angry Bots game with is exactly Diablo… it’s available as a free game on the asset store.

I think the new nightmares demo for Unity5 is also kinda like this but I’m not 100% sure…

angry bots dont work anymore… i try it, the problem with nightmares is that the shoot method dont instantiate a prefabe, only renders a line… but im cheking everythings possible :wink: