Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Consul-Livius · Jan 15, 2018 at 03:11 PM · c#gameobjectinstantiateprefabfor-loop

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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 :).

https://streamable.com/irw7k

alt text


untitled.png (219.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Consul-Livius · Jan 19, 2018 at 09:12 AM 0
Share

Thanks a lot dude!

avatar image
0

Answer by KittenSnipes · Jan 21, 2018 at 11:38 AM

@Consul-Livius

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;
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

451 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges