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 TheShadyColombian · Oct 09, 2014 at 06:43 PM · gameobjectif

if(object exists in scene)

I have a spawn script and i want waves to start ONLY if there are no other enemies present in the scene, but i can't find a way to check if an enemy is instantiated. Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class CrappyEnemySpawner : MonoBehaviour {
     public GameObject Enemy;
     public bool Enabled;
     public GameObject SpawnPoof;
     public float SpawnChanceIsOneIn;
     public float CurrentWave;
     public float EnemiesPerRound;
     public float WaveStartDelay;
     public float WaveDelay;
     public bool Wave;
     public GameObject SpawnedEnemy;
 
     // Use this for initialization
     void Start () {
         SpawnedEnemy = GameObject.FindGameObjectWithTag("Enemy");
         CurrentWave = 1;
         Wave = true;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (SpawnedEnemy.activeInHierarchy){
             Debug.Log ("No Enemies");
         }
 
         if (Wave == false){
             WaveStartDelay += 1    ;
         }
 
         if (WaveStartDelay > WaveDelay){
             Wave = true;
         }
 
         if (SpawnChanceIsOneIn<50 && Wave == true){
             SpawnChanceIsOneIn -= 3f;
         } else {
             SpawnChanceIsOneIn -= 0.1f;
         }
          if(Random.Range(1,100)>SpawnChanceIsOneIn && Enabled==true && Wave == true){
                 Instantiate(Enemy, transform.position, transform.rotation);
         }
 
         if(SpawnChanceIsOneIn<EnemiesPerRound && Wave == true){
             Debug.Log ("Congrats! You've made it to wave" + CurrentWave);
             CurrentWave += 1f;
             WaveDelay *= 1f;
             Wave = false;
             EnemiesPerRound -= 1.5f;
             SpawnChanceIsOneIn=100;
         }
     }
 }


Thanks in advance!

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

2 Replies

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

Answer by Kiwasi · Oct 09, 2014 at 06:57 PM

I do this by making all enemies children of the same GameObject. Then you can check childcount.

Another viable alternative is to add each enemy to a list and check the length of the list

Comment
Add comment · Show 11 · 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 TheShadyColombian · Oct 09, 2014 at 06:58 PM 0
Share

how would i go around adding them to a list

avatar image TheShadyColombian · Oct 09, 2014 at 09:08 PM 0
Share

I've heard of "GameObject.ActiveInHierarchy" how would i use it and would it work for this?

avatar image Kiwasi · Oct 09, 2014 at 10:32 PM 0
Share

Check out the docs. ActiveInHierachy just checks weather a GameObject is enabled or disabled (for various reasons it does not make sense to check a bool directly). Its probably not going to be much use to you.

avatar image Kiwasi · Oct 09, 2014 at 10:36 PM 0
Share

Here is some pseudo code on using a List

 public class Enemy {
 
     public static List<GameObject> enemyList = new List<GameObject>();
 
     void OnEnable (){
         enemyList.Add(gameObject);
     }
     
     void OnDisable (){
         enemyList.Remove(gameObject);
     }
 }
 
 public class SomeOtherClass {
 
     void Update (){
         Debug.Log(Enemy.enemyList.Count);
     }
 }

avatar image revolute · Oct 10, 2014 at 02:08 AM 1
Share

Just cast your object to GameObject.

Show more comments
avatar image
1

Answer by ThePunisher · Oct 09, 2014 at 10:44 PM

It's actually a very simple thing to do. In the script that is instantiating them (CrappyEnemySpawner in this case lol) you will want to keep track of all the spawned enemies by adding to directly to a list after they get instantiated. Like this:

 // List definition. This will track the enemies that have been spawned.
 private List<GameObject> m_enemies = new List<GameObject>();
 
 // Place where the enemy gets instantiated and added to the list.
 GameObject enemy = Instantiate(Enemy, transform.position, transform.rotation);
 m_enemies.Add(enemy);
 
 // When an enemy dies and you destroy it you will want to remove it from the list.
 m_enemies.Remove(enemyThatJustDied);
 Destroy(enemyThatJustDied);
 
 // Here is how you would check if there are no more enemies left.
 If(m_enemies.Count <= 0)
 {
    //All enemies have died, execute code in here.
 }
Comment
Add comment · Show 6 · 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 TheShadyColombian · Oct 10, 2014 at 12:56 AM 0
Share

List doesn't appear to exist

avatar image TheShadyColombian · Oct 10, 2014 at 12:57 AM 0
Share

and my enemies get destroyed on their own, so will i still need the "Remove from list" part because i'm kind of finding it a bit questionable, as when it is destroyed it won't exist so the list won't find if (or will it just give an error

avatar image TheShadyColombian · Oct 10, 2014 at 01:00 AM 0
Share

ooooh, you forgot to tell me to add using "System.Collections.Generic;"

avatar image TheShadyColombian · Oct 10, 2014 at 01:12 AM 0
Share

but it doesn't seem to recognize the number of characters spawned

avatar image Kiwasi · Oct 10, 2014 at 02:27 AM 0
Share

Sorry about the using statement. I use it so often I've put it in the defaults, and tend to forget to tell people about it.

Show more comments

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

30 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unexpected token: if 1 Answer

Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers

How to tell if two blocks are right next to each other?(2D) 1 Answer

Proper way to Pool Object - Can't acces due to protection level 1 Answer


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