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 Silentones · May 02, 2019 at 06:39 AM · player2d-platformerattackdeathspawning-enemies

[UNITY 2D] Can I create a spawner that spawns another enemy after its death using IF statements?

Will this work or would i need something else? I am taking a udemy course and the TA said that i can not use IF statements because it's not a number and I am dealing with gameobjects.

ALSO Will this work if i destroythe gameobject after a certain amount of time? at the moment my player is able to attack enemies.

     public GameObjecct Enemy;
     public float spawnTime;
     private float timeReset;
     
     private void Start ()
     {
           timeReset = spawnTime;
     }
     
     void Update()
     {
          spawnTime -= 2 * Time.detalTime;
     
          if (enemyHealth <= 0)
        {
             Instantiate (Enemy, transform.position, Quaternion.identity);
             timeReset = 0;
        }
     }
 
 

Comment
Add comment · Show 12
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 KevRev · May 02, 2019 at 07:41 AM 1
Share

It depends what you are doing with the enemy GameObjects once they hit zero health. If you are destroying them just as an if statement to count the number of enemies and only spawn if you have less than the maximum.

      public GameObjecct Enemy;
      public float spawnTime;
      private float timeReset;
      private int $$anonymous$$axEnemies=10;
     private int EnemyCount;
 
      private void Start ()
      {
            timeReset = spawnTime;
      }
      
      void Update()
      {
           spawnTime -= 2 * Time.detalTime;
       
           if (enemyHealth <= 0 && EnemyCount<$$anonymous$$axEnemies)
         {
              Instantiate (Enemy, transform.position, Quaternion.identity);
              timeReset = 0;
              EnemyCount+=1;
         }
      }
  
 // In your code that destroys the enemies
 EnemyCount-=1;

If you don't destroy them, you'll want to just adjust the $$anonymous$$axEnemies count at the point they hit zero health ins$$anonymous$$d. $$anonymous$$ake sure it's a trigger though, so they don't continually adjust the count whilst they remain at 0.

avatar image KevRev KevRev · May 03, 2019 at 08:19 AM 0
Share

Did this help? If so, please mark as the best answer.

avatar image Silentones KevRev · May 09, 2019 at 01:43 AM 0
Share

I can't mark this as anything it says reply =/ and up vote

Show more comments
avatar image Bonfire-Boy · May 03, 2019 at 09:58 AM 0
Share

What is enemyHealth? You don't seem to have declared it anywhere and it's absolutely critical to following what you're doing here.

Assu$$anonymous$$g enemyHealth is an int then there's nothing obviously logically wrong with the code. But also not nearly enough information to be able to evaluate what it does.

avatar image Silentones Bonfire-Boy · May 09, 2019 at 01:45 AM 0
Share

Sorry, i've been busy. This code is just an idea of what im trying to do with the game i'm trying to make.

Yes, EnemyHealth is going to be an int.

avatar image Bonfire-Boy Silentones · May 09, 2019 at 06:28 AM 0
Share

In that case there doesn't appear to be anything wrong with it. You will need to get clarification from your TA because what you've told us that they've said, makes no sense whatsoever.

But if the code that you're showing us is not the code that prompted their comment then clearly you are already misrepresenting them. Why not show us that code?

Show more comments
avatar image Bonfire-Boy · May 03, 2019 at 10:00 AM 0
Share

Also, "i can not use IF statements because it's not a number" makes no sense at all.

The only data type that is guaranteed to appear when using an if statement is bool (which is not a number), because the parentheses following the if have to contain something that can be resolved to a bool.

Are you sure that's what they said? I suspect there was more to it than that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by galactichyperstar · May 04, 2019 at 11:45 PM

Try this:

 public GameObject EnemyPrefab;
 public GameObject CurrentEnemy;
 
 void Update() {
 
     if (!CurrentEnemy) {
         CurrentEnemy = Instantiate(EnemyPrefab, transform.position, Quaternion.identity);
     }
 }

It will spawn an enemy as soon as the one it spawned is destroyed.

I am taking a udemy course and the TA said that i can not use IF statements because it's not a number and I am dealing with gameobjects.

If your TA said this, they are wrong!

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

136 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

Related Questions

Animation of the death (2D Game) 0 Answers

Help with InstantDeath Collider Script? 1 Answer

How to stop enemy within certain distance of player? 2 Answers

Child of a player not syncing position with network? 0 Answers

Why is my character taking damage when attacking & why is it attacking multiple times per hit? 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