Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Health Increase when a PreFab is destroyed

Discussion in 'Scripting' started by brashadam85, Feb 26, 2021.

  1. brashadam85

    brashadam85

    Joined:
    Jan 27, 2021
    Posts:
    14
    I have a little question, probably really straight forward but I'm still very much a beginner and just struggling a little with this.

    I have 2 enemies that spawn and i am using a script attached to my projectile to destroy the projectile and the other object it hits using the following script


    Code (CSharp):
    1.     public void OnTriggerEnter(Collider other)
    2.     {
    3.         Destroy(gameObject);
    4.         Destroy(other.gameObject);
    5.  
    6.         FindObjectOfType<Score>().IncreaseScore();
    7.  
    8.        
    9.      
    10.  
    11.     }
    what I am trying to do is when one of the enemy types is destroyed i want to increase the players health. I have the players health decrease when the enemies collide so all the info is there, just now sure how to implement it.

    Any help with this would be greatly appreciated.
     
  2. dazaiUwU

    dazaiUwU

    Joined:
    Sep 10, 2020
    Posts:
    5
    First of all use Destroy(gameObject); at the end. Code Execution stops once gameobject is destroyed. Get The reference to player just like you did for score FindObjectOfType<Player>().IncreaseHealth();

    1. public void OnTriggerEnter(Collider other)
    2. {
    3. FindObjectOfType<Score>().IncreaseScore();
    4. FindObjectOfType<Player>().IncreaseHealth();
    5. Destroy(other.gameObject);
    6. Destroy(gameObject);
    • }
     
  3. brashadam85

    brashadam85

    Joined:
    Jan 27, 2021
    Posts:
    14
    Thanks for reply, so I'll just need to make another script to handle health increase then call upon here, how would I make it so only one of my 2 enemy prefabs increase health, as I think this script will increase health on every enemy killed. Will i just need to do a another script to split them up?
     
  4. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    you implement the logic inside Player script which it has IncreaseHealth();
     
  5. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    Player script add a bool call noHealForNextKill
    and it geos somting like this :
    Code (CSharp):
    1. public IncreaseHealth()
    2. {
    3.     if(!noHealForNextKill)
    4.     {
    5.         //do some IncreaseHealth
    6.     }
    7.  
    8.     noHealForNextKill = (noHealForNextKill)? false:true;
    9. }
     
  6. brashadam85

    brashadam85

    Joined:
    Jan 27, 2021
    Posts:
    14
    I've managed to get my health to increase on a key press just now which is a step in the right direction, will slowly get there from that.

    Thanks for replies, the community here is fantastic, especially for a noob like me. I'm really enjoying Unity and what I've managed to learn and create in such a short time. Looking forward to diving deeper into the world C# and continuing to improve.
     
    Last edited: Feb 27, 2021
    Putcho likes this.
  7. brashadam85

    brashadam85

    Joined:
    Jan 27, 2021
    Posts:
    14
    I'm still driving myself mad trying to figure this out, I've got as far as my health increasing when I press the F key, which is progress I guess.

    I have a player that has a Controller Script and a Damage Script, the damage script is where I've put the health increase, this is also where the player damage and death is handled.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class DamagePlayer : MonoBehaviour
    8. {
    9.     public float playerHealth = 500f;
    10.     public float currentHealth;
    11.     public float damage = 0.4f;
    12.     public float healthUp = 50f;
    13.     public GameObject redEnemy;
    14.  
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         print(playerHealth);
    20.     }
    21.  
    22.  
    23.     public void FixedUpdate()
    24.     {
    25.         if (Input.GetKeyDown(KeyCode.F))
    26.  
    27.         {
    28.                      playerHealth +=  healthUp + currentHealth;
    29.         }
    30.     }
    31.       void OnCollisionStay(Collision collision)
    32.     {
    33.         if (collision.gameObject)
    34.         {
    35.             playerHealth -= damage;
    36.             print("Im HIT" + playerHealth);
    37.         }
    38.    
    39.         //Activating public bool IsDead function when player IsDead = True
    40.         if (IsDead() == true)
    41.         {
    42.             playerHealth = 0;
    43.             Destroy(gameObject);
    44.             ActionAfterDelay.Create(2.5f, () => { SceneManager.LoadScene("GameOver"); });
    45.      
    46.         }
    47.  
    48.     }
    49.  
    50.     public bool IsDead()
    51.     {
    52.         //Player health less than 1 player is dead = true
    53.         return playerHealth < 1;
    54.  
    55.      
    56.     }
    57.  
    58. }
    59.          
    60.  
    I have a Spawn Manager Script which spawns 2 enemies, A basic (white) and a large (red). The player fires a projectile that has a script to deal with destroying the projectile and the enemy it hits.
    What I'm really struggling with is how to detect if I've killed the red enemy and make my health increase instead of pressing a key. I've managed to build this whole little game from scratch the past week and this is the only little thing that is holding me back. I'm sure to the trained eye the answer is simple but I'm completely baffled. I've looked at many a thread and video and seen many ways to implement health systems, and but just struggling to get anything working on this.
     
    Last edited: Feb 27, 2021