Hello, I was wondering if someone could help me with a script problem. My stamina doesn't work whenever I'm running, the stamina is supposed to decrease. My PlayerStats: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerStats : MonoBehaviour { [SerializeField] private Image health_Stats, stamina_Stats; public void Display_HealthStats(float healthValue) { healthValue /= 100f; health_Stats.fillAmount = healthValue; } public void Display_StaminaStats(float staminaValue) { staminaValue /= 100f; stamina_Stats.fillAmount = staminaValue; } } My HealthScript: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class HealthScript : MonoBehaviour { private EnemyAnimator enemy_Anim; private NavMeshAgent navAgent; private EnemyController enemy_Controller; public float health = 100f; public bool is_Player, is_Boar, is_Cannibal; private bool is_Dead; private EnemyAudio enemyAudio; private PlayerStats player_Stats; void Awake () { if(is_Boar || is_Cannibal) { enemy_Anim = GetComponent<EnemyAnimator>(); enemy_Controller = GetComponent<EnemyController>(); navAgent = GetComponent<NavMeshAgent>(); // enemy audio in inspector toevoegen enemyAudio = GetComponentInChildren<EnemyAudio>(); } if(is_Player) { //is_Player aanvinken in inspector player_Stats = GetComponent<PlayerStats>(); } } public void ApplyDamage(float damage) { if (is_Dead) return; health -= damage; if(is_Player) { player_Stats.Display_HealthStats(health); } if(is_Boar || is_Cannibal) { //is_Cannibal aanvinken in inspector if(enemy_Controller.Enemy_State == EnemyState.PATROL) { enemy_Controller.chase_Distance = 50f; } } if(health <= 0f) { PlayerDied(); is_Dead = true; } } void PlayerDied() { if(is_Cannibal) { GetComponent<Animator>().enabled = false; GetComponent<BoxCollider>().isTrigger = false; GetComponent<Rigidbody>().AddTorque(-transform.forward * 5f); enemy_Controller.enabled = false; navAgent.enabled = false; enemy_Anim.enabled = false; StartCoroutine(DeadSound()); // EnemyManager spawn more enemies EnemyManager.instance.EnemyDied(true); } if(is_Boar) { navAgent.velocity = Vector3.zero; navAgent.isStopped = true; enemy_Controller.enabled = false; enemy_Anim.Dead(); StartCoroutine(DeadSound()); // EnemyManager spawn more enemies EnemyManager.instance.EnemyDied(false); } if(is_Player) { GameObject[] enemies = GameObject.FindGameObjectsWithTag(Tags.ENEMY_TAG); for (int i = 0; i < enemies.Length; i++) { enemies[i].GetComponent<EnemyController>().enabled = false; } EnemyManager.instance.StopSpawning(); GetComponent<PlayerMovement>().enabled = false; GetComponent<PlayerAttack>().enabled = false; GetComponent<WeaponManager>().GetCurrentSelectedWeapon().gameObject.SetActive(false); } if(tag == Tags.PLAYER_TAG) { Invoke("RestartGame", 3f); } else { Invoke("TurnOffGameObject", 3f); } } // player is dood void RestartGame() { UnityEngine.SceneManagement.SceneManager.LoadScene("SampleScene"); } void TurnOffGameObject() { gameObject.SetActive(false); } IEnumerator DeadSound() { yield return new WaitForSeconds(0.3f); enemyAudio.Play_DeadSound(); } }
I see you are making a call to Display_HealthStats when you take damage, which is great, but I don't see you ever calling Display_StaminaStats. But then, the second script you show is a health script, so I'm not sure that I'd expect changes to Stamina to be made from that script. Where do you call Display_StaminaStats from?
Hi, If I'm correct the Display_Stats is called in my sprintandcrouch script and the stamina is supposed to decrease when i hold Left shift and walk/run i think. The thing is it doesn't work. It might be tied to my other problem: pressing or holding buttons to shoot or run etc doesnt work. The sprint code is: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerSprintAndCrouch : MonoBehaviour { private PlayerMovement playerMovement; public float sprint_Speed = 10f; public float move_Speed = 5f; public float crouch_Speed = 2f; private Transform look_Root; private float stand_Height = 1.6f; private float crouch_Height = 1f; private bool is_Crouching; private PlayerFootsteps player_Footsteps; private float sprint_Volume = 1f; private float crouch_Volume = 0.1f; private float walk_Volume_Min = 0.2f, walk_Volume_Max = 0.6f; private float walk_Step_Distance = 0.4f; private float sprint_Step_Distance = 0.25f; private float crouch_Step_Distance = 0.5f; private PlayerStats player_Stats; private float sprint_Value = 100f; public float sprint_Treshold = 10f; void Awake() { playerMovement = GetComponent<PlayerMovement>(); look_Root = transform.GetChild(0); player_Footsteps = GetComponentInChildren<PlayerFootsteps>(); player_Stats = GetComponent<PlayerStats>(); } void Start() { player_Footsteps.volume_Min = walk_Volume_Min; player_Footsteps.volume_Max = walk_Volume_Max; player_Footsteps.step_Distance = walk_Step_Distance; } // Update is called once per frame void Update() { Sprint(); Crouch(); } void Sprint() { // if we have stamina we can sprint if (sprint_Value > 0f) { if (Input.GetKeyDown(KeyCode.LeftShift) && !is_Crouching) { playerMovement.speed = sprint_Speed; player_Footsteps.step_Distance = sprint_Step_Distance; player_Footsteps.volume_Min = sprint_Volume; player_Footsteps.volume_Max = sprint_Volume; } } if (Input.GetKeyUp(KeyCode.LeftShift) && !is_Crouching) { playerMovement.speed = move_Speed; player_Footsteps.step_Distance = walk_Step_Distance; player_Footsteps.volume_Min = walk_Volume_Min; player_Footsteps.volume_Max = walk_Volume_Max; } if (Input.GetKey(KeyCode.LeftShift) && !is_Crouching) { sprint_Value -= sprint_Treshold * Time.deltaTime; if (sprint_Value <= 0f) { sprint_Value = 0f; // reset the speed and sound playerMovement.speed = move_Speed; player_Footsteps.step_Distance = walk_Step_Distance; player_Footsteps.volume_Min = walk_Volume_Min; player_Footsteps.volume_Max = walk_Volume_Max; } player_Stats.Display_StaminaStats(sprint_Value); } else { if (sprint_Value != 100f) { sprint_Value += (sprint_Treshold / 2f) * Time.deltaTime; player_Stats.Display_StaminaStats(sprint_Value); if (sprint_Value > 100f) { sprint_Value = 100f; } } } } // sprint void Crouch() { if (Input.GetKeyDown(KeyCode.C)) { // if we are crouching - stand up if (is_Crouching) { look_Root.localPosition = new Vector3(0f, stand_Height, 0f); playerMovement.speed = move_Speed; player_Footsteps.step_Distance = walk_Step_Distance; player_Footsteps.volume_Min = walk_Volume_Min; player_Footsteps.volume_Max = walk_Volume_Max; is_Crouching = false; } else { // if we are not crouching - crouch look_Root.localPosition = new Vector3(0f, crouch_Height, 0f); playerMovement.speed = crouch_Speed; player_Footsteps.step_Distance = crouch_Step_Distance; player_Footsteps.volume_Min = crouch_Volume; player_Footsteps.volume_Max = crouch_Volume; is_Crouching = true; } } // if we press c } // crouch } // class
I would suggest adding some Debug.Log statements through your sprint code and see if it's executing the parts you think it should. Then add another to your Display_StaminaStat method and see if that is executing. If your debug messages print out, then at least you know the code is running. But wouldn't hurt to check out what @Elango mentioned first as that would be a quick check.