Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

solved regeneration

Discussion in 'Scripting' started by TheCodingNoob, Jan 27, 2020.

Thread Status:
Not open for further replies.
  1. TheCodingNoob

    TheCodingNoob

    Joined:
    Jan 10, 2020
    Posts:
    14
    Hej guys and girls I'm trying to get a regeneration system on my health script but it is only throwing errors at me. I'm trying to learn and experiment on my own but I can't get it to work.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerHealth : MonoBehaviour
    7. {
    8.     public int startingHealth = 100;                            // The amount of health the player starts the game with.
    9.     public int currentHealth;                                   // The current health the player has.
    10.     public Slider healthSlider;                                 // Reference to the UI's health bar.
    11.     public Image damageImage;                                   // Reference to an image to flash on the screen on being hurt.
    12.     public AudioClip deathClip;                                 // The audio clip to play when the player dies.
    13.     public float flashSpeed = 5f;                               // The speed the damageImage will fade at.
    14.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     // The colour the damageImage is set to, to flash.
    15.     MouseLook playerLook;
    16.    
    17.  
    18.  
    19.     Animator anim;                                              // Reference to the Animator component.
    20.     AudioSource playerAudio;                                    // Reference to the AudioSource component.
    21.     PlayerMovment playerMovement;                              // Reference to the player's movement.
    22.     Gun playerShooting;                              // Reference to the PlayerShooting script.
    23.     bool isDead;                                                // Whether the player is dead.
    24.     bool damaged;                                               // True when the player gets damaged.
    25.     float Regeneration;
    26.    
    27.  
    28.     void Awake()
    29.     {
    30.         // Setting up the references.
    31.         anim = GetComponent<Animator>();
    32.         playerAudio = GetComponent<AudioSource>();
    33.         playerMovement = GetComponent<PlayerMovment>();
    34.         playerShooting = GetComponentInChildren<Gun>();
    35.         playerLook = GetComponent<MouseLook>();
    36.  
    37.         // Set the initial health of the player.
    38.         currentHealth = startingHealth;
    39.     }
    40.  
    41.  
    42.     void Update()
    43.     {
    44.         // If the player has just been damaged...
    45.         if (damaged)
    46.         {
    47.             // ... set the colour of the damageImage to the flash colour.
    48.             damageImage.color = flashColour;
    49.         }
    50.         // Otherwise...
    51.         else
    52.         {
    53.             // ... transition the colour back to clear.
    54.             damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    55.         }
    56.  
    57.        
    58.        
    59.  
    60.         // Reset the damaged flag.
    61.         damaged = false;
    62.     }
    63.  
    64.    
    65.  
    66.  
    67.     public void TakeDamage(int amount)
    68.     {
    69.         // Set the damaged flag so the screen will flash.
    70.         damaged = true;
    71.  
    72.         // Reduce the current health by the damage amount.
    73.         currentHealth -= amount;
    74.  
    75.         // Set the health bar's value to the current health.
    76.         healthSlider.value = currentHealth;
    77.  
    78.  
    79.         // Play the hurt sound effect.
    80.         playerAudio.Play();
    81.  
    82.         // If the player has lost all it's health and the death flag hasn't been set yet...
    83.         if (currentHealth <= 0 && !isDead)
    84.         {
    85.             // ... it should die.
    86.             Death();
    87.         }
    88.     }
    89.  
    90.  
    91.     void Death()
    92.     {
    93.         // Set the death flag so this function won't be called again.
    94.         isDead = true;
    95.  
    96.  
    97.         StartCoroutine(IsDead());
    98.     }
    99.  
    100.     IEnumerator IsDead()
    101.     {
    102.         playerAudio.clip = deathClip;
    103.         playerAudio.Play();
    104.         yield return new WaitForSeconds(.15f);
    105.  
    106.         SceneManager.LoadScene(1);
    107.  
    108.     }
    109.  
    110.     IEnumerator Regeneration()
    111.     {
    112.         yield return new WaitForSeconds(2f);
    113.         currentHealth += 1;
    114.     }
    115.  
    116.     void damagetaken()
    117.     {
    118.         if(currentHealth <= startingHealth)
    119.         {
    120.             Debug.Log("healing");
    121.             StartCoroutine(Regeneration());
    122.         }
    123.     }
    124. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    What doesn't work?
    Please describe your problem in more detail,
    What should happen and when.
    And what happens instead.
    Put Debug.Log in more places, to track issues.
     
  3. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1.  
    2. //Maybe you need to call
    3. StartCoroutine(Regeneration());
    4. //in your awake
    5.  
     
Thread Status:
Not open for further replies.