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 ecequalsm2 · Mar 25, 2016 at 04:49 AM · enemydestroygameobjectspawnerwaypoint systemwave

simple waypoint system enemy wave spawner

I can't get the enemies (gameObjects) to delete. I've tried setting them up to delete if they collided with a preset destroyer box beneath the game screen and I've tried to have them destroy themselves if they collide with the player or player bullets. Since my wave spawning was contingent upon the first wave being either destroyed by the player or the GameManager (if they go off screen), I tried another script that looked a bit more promising. But now, I'm stuck on one part of my code that I can't figure out. Can you help me figure out what I'm doing wrong in this script, so I can set the waves of enemies to continue without overtaxing my CPU?

It's all between two scripts that I'm trying to accomplish the enemy wave spawner function. Here's my WaveSpawner script:

using UnityEngine; using System.Collections; using System.Collections.Generic; using SWS;

public class WaveSpawner : MonoBehaviour {

public enum SpawnState { Spawning, Waiting, Counting}

;

[System.Serializable] public class Wave { public string name; public GameObject enemyGO; public int count; public float rate; }

public Wave[] waves; private int nextWave = 0;

public float timeBetweenWaves = 5f; public float waveCountdown;

public splineMove eMove;

private float searchCountdown = 1f;

private SpawnState state = SpawnState.Counting;

void Start () { waveCountdown = timeBetweenWaves; }

void Update () { if (state == SpawnState.Waiting) { //Check if player has killed all enemies if (!EnemyIsAlive ()) { //Begin a new wave Debug.Log("Wave Complete!"); return; } else { return; }

   }
   if (waveCountdown <= 0) {
      if (state != SpawnState.Spawning) {

         //Start spawning wave
         StartCoroutine (SpawnWave (waves [nextWave]));
      }
   } else {

      waveCountdown -= Time.deltaTime;
   }
      

}

bool EnemyIsAlive () { searchCountdown -= Time.deltaTime; if (searchCountdown <= 0f) { searchCountdown = 1f; if (GameObject.FindGameObjectWithTag ("Enemy") == null) { return false; } } return true; }

IEnumerator SpawnWave (Wave _wave) { //Spawn Debug.Log("Spawning Wave: " + _wave.name); state = SpawnState.Spawning;

   for (int i = 0; i < _wave.count; i++) {
      SpawnEnemy (_wave.enemyGO);
      yield return new WaitForSeconds (1f / _wave.rate);
   }

   state = SpawnState.Waiting;

   yield break;

}

void SpawnEnemy (GameObject _enemy) {

   string[] myPaths = new string[] { "Path 1", "Path 2", "Path 3", "Path 4", "Path 5" };
   //create reference to Waypoint Manager, specifying which path to use
   PathManager myPath = WaypointManager.Paths["Path 1"];


   Debug.Log ("Spawning Enemy: " + _enemy.name);


   //create a new copy of the _enemy and make it equal to _enemy at the following parameters
   GameObject newEnemy = (GameObject)Instantiate(_enemy, myPath.waypoints[0].position, Quaternion.identity);

   //set eMove to that new copy of _enemy (newEnemy) and get splineMove script to reference its movement path
   eMove = newEnemy.GetComponent<splineMove> ();

   //set the path of eMove to Path 1 of WaypointManager's preset paths
   eMove.SetPath(myPath);


} }

And here's my Enemy Script:

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour {

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () { //Get the enemy current position Vector2 _enemyPos = transform.position;

   //the bottom-left point of the screen
   Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2 (0,0));

   //if enemy went outside the screen on the bottom, then destroy the enemy
   if (_enemyPos.y < min.y) {
      //GameManager.KillEnemy (this);
      Destroy (gameObject);
   }

}

//Made this OnTriggerEnter2D function since the function above wouldn't destroy the enemy gameObjects void OnTriggerEnter2D(Collider2D col){ if ((col.tag == "Player") || (col.tag == "Destroyer")) { Destroy (gameObject); } } }

As a backup, I made this Destroyer Script and attached to an empty GameObject with a Box Collider2D that covers the width of the bottom of the screen and is set to IsTrigger. Here it is:

using UnityEngine; using System.Collections;

public class Destroyer : MonoBehaviour {

void OnTriggerEnter2D(Collider2D col){ if (col.tag == "Enemy") { Destroy (gameObject); } } }

None of it is destroying the enemy ships. What am I doing wrong?

Thanks in advance, Millie

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
1

Answer by LazyElephant · Mar 25, 2016 at 04:53 AM

In your Destroyer class, when you say Destroy(gameobject), you are destroying the game object which the Destroyer script is attached to. If you want it to instead destroy the enemies, it should be

 Destroy(col.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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My wave system isn't working 0 Answers

Spanwer Script only spawns when Player is close and more. 1 Answer

Next enemy wave timer issue 0 Answers

Access a instantiated gameobject from a enemy spawner 4 Answers

Spawn "n" Enemies Every "nth" Wave? 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