- Home /
How to make a gameObject instantiate a prefab for every 250 damage taken ?
Hi id like to know how do I make an efficient code for this, i was thinking a for loop for every 250 damage taken, the enemy will instantiate a prefab. But in a noob to coding so if anyone can tell me how do I achieve this, would be a great help, thank you :)
Answer by kaplica · Jan 15, 2018 at 03:23 PM
I would suggest using an event so that when each time damage is taken you can instantiate prefab.
A tutorial from unity: https://unity3d.com/learn/tutorials/topics/scripting/events
Answer by Hellium · Jan 15, 2018 at 03:30 PM
It's really hard to help without the code you've made so far.
Here is how I would do it :
 private int life ;
 private int lifeThreshold = 250;
 
 public void TakeDamage( int damages )
 {
     life -= damages ;
     lifeThreshold -= damages ;
 
     if( life <= 0 )
     {
         life = 0 ;
         lifeThreshold = 0 ;
         Die();
     }
     else if( lifeThreshold <= 0 )
     {
          while( lifeThreshold <= 0 )
              lifeThreshold += 250 ;
          // Instantiate your gameobject
     }
 }
Answer by PersianKiller · Jan 15, 2018 at 03:50 PM
hey watch this it's very simple and nice.
you should have a healthManager script like this
          using System.Collections;
            using System.Collections.Generic;
            using UnityEngine;
               public class healthManager : MonoBehaviour {
 public int health;
 public int itsTimeToInstantiate;
 public GameObject bloodParticle;
 // Use this for initialization
 public void GiveDamage(int a){//we check this later
     health-=a;
     itsTimeToInstantiate += a;
     if (itsTimeToInstantiate > 250) {
         itsTimeToInstantiate = 0;
         Instantiate (bloodParticle,transform.position,transform.rotation);
         //instantiate blood 
         //now lets create a blood particle
     }
 }
       }
and a spike like this
          using System.Collections;
           using System.Collections.Generic;
         using UnityEngine;
          public class spike : MonoBehaviour {
 void OnTriggerEnter2D(Collider2D other){
     if (other.CompareTag ("player")) {
         other.GetComponent<healthManager> ().GiveDamage (100);//so it will damage player 50 points
         Destroy(gameObject);
     }
         
 }
        }
it's like the tutorial watch this it will help you :).

Answer by KittenSnipes · Jan 21, 2018 at 11:38 AM
This is a script that performs each time we are damaged for 250 or more:
     //The amount player is damaged before an event plays.
     public float damageInterval = 250;
 
     //The maxHealth of the player.
     public float maxHealth = 1000;
 
     //Amount of damage done to player using the hurt key.
     public float hurtAmount = 100;
 
     //Key in which when clicked hurts the player.
     public KeyCode HurtKey = KeyCode.Q;
 
     //HealthSlider on a canvas to mark how much health the player has.
     public UnityEngine.UI.Slider healthSlider;
 
     //The currentHealth reference of the player.
     float currentHealth;
 
     //This is used to get the damage the player needs 
     //to be damaged before an event plays.
     float currentDamageInterval;
 
     //This is a bool telling whether a critical is allowed or not.
     bool criticalAllowed;
 
     void Start () {
         //Set criticalAllowed to false because we can not do a critical on start.
         criticalAllowed = false;
 
         //Set currentHealth to maxHealth.
         currentHealth = maxHealth;
 
         //The damage needed for an event to play. So it is (1000 - 250) currently.
         currentDamageInterval = maxHealth - damageInterval;
 
         //The amount needed for the first critical hit or whatever event you want.
         Debug.Log("First Critical: " + currentDamageInterval);
 
         //Sets the slider to have the maxHealth.
         healthSlider.maxValue = maxHealth;
 
         //Then sets its value to currentHealth.
         healthSlider.value = currentHealth;
     }
 
     void Update () {
         //If the hurt key is pressed.
         if (Input.GetKeyDown(HurtKey))
         {
             //Then damage the player for the hurt amount.
             TakeDamage(hurtAmount);
         }
 
         //If the players health does not equal our maxHealth 
         //and is not less than or equal to 0.
         if (currentHealth != maxHealth && !(currentHealth <= 0))
         {
             //And our player's currentHealth is less than or equal to our damage interval.
             if (currentHealth <= currentDamageInterval)
             {
                 //You can play the event right here.
                 //I made one up it does a critical hit.
                 CriticalHit();
 
                 //Setup our damage interval for the next event. 
                 //The first time this will be set as (750 - 250).
                 //The second time (500 - 250).
                 //And the last (250 - 250).
                 currentDamageInterval -= damageInterval;
 
                 //Say ouch because we want to know the event played.
                 Debug.Log("Ouch!");
 
                 //This tells you how much health we need for the next event.
                 Debug.Log("DamageTil Next Critical: " + currentDamageInterval);
             }
         }
     }
 
     void CriticalHit()
     {
         //This find a number between 0-10 that is needed for the critical to work.
         int numberNeededForHit = Random.Range(0, 10);
 
         //This will be a random guess between 0-10
         int guessAtCriticalHitNumber = Random.Range(0, 10);
 
         //If our random guess is equal to the number needed for the critical.
         if (guessAtCriticalHitNumber == numberNeededForHit)
         {
             //Allow for the critical to be performed.
             criticalAllowed = true;
         }
 
         else
         {
             //If our random guess is wrong say so.
             Debug.Log("Our Critical Attack Failed!");
         }
     }
 
     void TakeDamage(float DamageAmount)
     {
         //If our currentHealth less than or equal to 0.
         if (currentHealth <= 0)
         {
             //Then set currentHealth equal to 0.
             currentHealth = 0;
 
             //Then return so nothing else happens from here.
             return;
         }
 
         //If we can perform our critical.
         if (criticalAllowed == true)
         {
             //This is our critical damage amount.
             float CriticalDamageAmount = DamageAmount * 2;
 
             //Subtract our health by our critical damage.
             currentHealth -= CriticalDamageAmount;
 
             //Then disallow for another critical because we just performed it.
             criticalAllowed = false;
         }
 
         //If we do not have a critical.
         if (criticalAllowed == false)
         {
             //Damage our player by the regular damage amount.
             currentHealth -= DamageAmount;
         }
 
         //Set our slider to show our current health.
         healthSlider.value = currentHealth;
     }
Your answer
 
 
             Follow this Question
Related Questions
How to create GameObject without adding it to scene? 1 Answer
Thrown objects always have same rotation 2 Answers
Spawning a prefab at another object's location 3 Answers
gameObject are not referenced 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                