• 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 JayFitz91 · Sep 06, 2014 at 08:05 PM · gameobjectforeachactive

Keeping one enemy object active at a time

I have a scene with a few different enemies in it, the scene starts with one enemy active with the others waiting around. I would like the next enemy in the array to come to life when the last enemy gets destroyed. Could anyone help me out with some logic for this situation as I'm a bit stumped at the moment.

I have this so far which keeps my first enemy active while the others are temporarily disabled:

Edit3

I'm heading out for a while, this is all the relevant code to the question, if anyone could help me out, i'd greatly appreciate it. I now have the number starting at 0 and increasing to 1 when the ship hits a bullet but, the Debug.Log in the Update, constantly outputs 0 at the beginning, but when my trigger is activated and the number changes to 1, it only shows it once and then I get nothing from the console when really it should repeat 1 constantly, just in case that's relevant:

     using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     
 
     public bool isActive = true;
 
     public int shipCount = 13;
 
     string shipName = "ship";
     int num = 0;
 
     void Start()
     {
         //Sets ship 0 to true, leaving the rest at false
         if(gameObject.tag == shipName + num)
         {
             isActive = true;
         }
         else
             isActive = false;
     }
 
     void Update()
     {
         Debug.Log (shipName + num);
         if(gameObject.tag == shipName + num && isActive == false)
         {
             isActive = true;
         }
         
         if(isActive == false)
         {
             GetComponent<EnemyAI>().enabled = false;
         }
         else
             GetComponent<EnemyAI>().enabled = true;
     }
 
     void OnTriggerEnter(Collider collide)
     {
         if(gameObject.tag == shipName + num && isActive == true)
         {
             if(collide.gameObject.tag == "bullet")
             {
                 isActive = false;
                 num++;
             }
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cdrandin · Sep 06, 2014 at 10:24 PM

This is how I would think about it. Have an Enemy AI script as you have one and then an Enemy manager.

So the Enemy AI script, controls the action of the object and let it do its thing. Ignoring whether it needs to be active at a certain time. Let the enemy manager take care of that.

Now the Enemy manager...

It contains the array of enemy objects. If an enemy dies, the Enemy AI script should notify the manager that it just died. Then from that point, the manager will then clean up the enemy or whatever. Then, gets the next available enemy, if any exist, then either add the component or better yet just pool* the already destroyed object. If these enemy's are not seen on the screen all at once, this would be cost efficient. Or just reuse the same Enemy AI component so you don't need to waste memory creating and destroying, again pooling*. Now, the Enemy is object don't forget to set up whatever other things you need and re-position if need be. Then let the enemy be.

This will continue to work until there are no more enemy in the container.

Hope this has helped.

* Here is a simple script I created to handle pooled objects.

Simple Object Pooling

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to find all similar elements in a list? 1 Answer

How to make an open and close box?,How to make open and close a box? 0 Answers

Script only works onetime 0 Answers

activating and deactivating gameObject on keypress? 1 Answer

Problem with foreach loop with GameObject array 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges