Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TRALLALAL · Apr 25, 2014 at 08:37 AM · randomenemydamagerespawnrandomspawning

Need help with enemy respawn script

Hello, this is the script. Basically, it's mixed with the enemy health script so it's easier for me, I want to spawn more enemies as soon as the group that spawned earlier dies, for example, the game starts with only one enemy, as soon as he's dead, I want two more enemies to spawn and when they are both dead I want to spawn three and so on. The problem with this script is that I want them to spawn on random positions but they always spawn on the same position and one at the time. Looking at the code the group of enemy would spawn when just one is killed and that's wrong, so, in short:

1) How can I spawn a group of enemies when the group that spawned earlier is dead? 2) How can I spawn the enemies in random positions?

This is the code I made

 public var damage = 15;
 private var health = 100;
 private var enemyNumber = 2;
 
 function Start () {
 
 }
 
 function Update () {
     if(health < 1) {
         animation.Play("death");
         Destroy(gameObject, 0.31);
     }
     Debug.Log(health);
     if(health < 0) {
         health = 0;
         respawn();
     }
 }
 
 function OnCollisionEnter (bulletCollision : Collision) {
     if(bulletCollision.gameObject.tag == "bullet") {
         health -= damage;
         Debug.Log("Hit enemy");
     }
 }
 
 function respawn() {
     var Enemy = GameObject.FindGameObjectWithTag("Enemy");
     var enemySpawns = GameObject.FindGameObjectsWithTag("spawns");
     for(var i = 0; i < enemyNumber; i++) {
         var spawn = Random.Range(0, enemySpawns.Length);
         Instantiate(Enemy, enemySpawns[spawn].transform.position, transform.rotation);
     }
     enemyNumber++;        
 }

EDIT: Just found out that more than one enemy spawn when one is killed, the problem is that by spawning on the same place they (I'm using cubes at the moment), let's say sum up and it seems like there is only one cube. As I said above, I want them to spawn when the group that spawned earlier is gone and I want them to spawn to random places.

EDIT2: The code remains the same but now the random spawns are working and it spawns more than one enemy at the time. Still, it spawns too many enemies and not when the group is dead.

Comment
Add comment · Show 2
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 darthtelle · Apr 25, 2014 at 09:17 AM 0
Share

Print out the positions which are stored inside enemySpawns. Are they different positions? Are there multiple values?

avatar image TRALLALAL · Apr 25, 2014 at 10:15 AM 0
Share

As I said in the second edit random spawns now are working. Still, too many enemies spawn when one is killed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by wijesijp · Apr 25, 2014 at 10:29 AM

If the code you post is enemy code then the respawn logic is wrong.

If you had 2 enemies then when each one gets killed will spawn 2 enemies. So you will have 4 enemies when 2 enemies get killed not 3 as you wanted. Also enemyNumber is a private variable it will not keep on increasing in the next enemy enemyNumber is still 2.

The best way to handle is not to put respawn logic inside enemy class. Create another class that handles enemy spawning (say EnemyMaker). When enemy get killed inform EnemyMaker that it got killed. When all the enemies get killed EnemyMaker can respawn next set of enemies.

 public class EnemyMaker : MonoBehaviour
 {
     public static EnemyMaker current;
 
     public GameObject enemyPrefab;
     private int enemyCount = 2;
     private int enemyForLevel = 2;
 
     void Awake()
     {
         current = this;
     }
 
     void Start()
     {
         Respawn();
     }
 
     void Respawn()
     {
         enemyCount = enemyForLevel;
         // respon code ....
     }
 
     public void EnemyIsDead()
     {
         enemyCount--;
     }
 
     void Update()
     {
         if (enemyCount == 0)
         {
             enemyForLevel++;
             Respawn();
         }
     }
 
 }

 
 


 public class Enemy : MonoBehaviour
     {
         private float health;
         void OnCollisionEnter(Collision bulletCollision)
         {
             // code to handle health
             if (health <= 0)
             {
                 EnemyMaker.current.EnemyIsDead();
                 Destroy(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

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

22 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

Related Questions

problem extracting transform from an array 1 Answer

Instantiate random object from Array 2 Answers

How do I animate my enemy randomly with time? 1 Answer

Respawned with Camera problem 2 Answers

Enemy deals damage to player on contact 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