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 croxfade · May 06, 2014 at 06:13 PM · c#respawnspaceenemiesarcade

How to respawn enemies after one is destroyed?

So I am doing this simple arcade style shooter game. My problem is that I know once my laser hits the enemy object it should get destroyed but once it get destroyed it is gone forever. I want to know how can I have a continuous enemy objects come at my spaceship even if it is destroyed. Its a 2d game but when I set up my game I followed the official unity3d beginner space shooter tutorial. In that tutorial coroutine were used but I am doing mine a bit differently. Here is how my code looks

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
     Transform myTransform;
     public int minSpeed;
     public int maxSpeed;
     int x,y,z;
     public int currentSpeed;
 
     // Use this for initialization
     void Start () {
         x = -7;
         y =  0;
         z = 14;
         myTransform = transform;
         myTransform.position = new Vector3(x,y,z);
         currentSpeed = Random.Range(minSpeed,maxSpeed);
     }
     
     // Update is called once per frame
     void Update () 
     {
             x= Random.Range(-12,12);
 
             myTransform.Translate(Vector3.back * currentSpeed * Time.deltaTime);
             //everytime the enemy object goes beyond z=-6 it dissapears and creates a new enemy object
             if(myTransform.position.z < -6)
             {
                 myTransform.position = new Vector3(x,y,z);
                 currentSpeed = Random.Range(minSpeed,maxSpeed);
             }
     }
     void OnTriggerEnter(Collider collider)
     {
         if(collider.gameObject.CompareTag("Projectile"))
         {
             Destroy(this.gameObject);
         }
     }
     
 }
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 robertbu · May 06, 2014 at 07:04 PM 2
Share

Out of context of your app, it is hard to say what is the right approach. You either have Instantiate() a new game object, or you have to reuse this one. If you elect to reuse, in some games you can just teleport it and reset any state you need. In other games, you need to have an object pool. Search UA for a number of answers for both simple and more complex pooling solutions.

While folks can give you opinions, as this questions stands now, there is not enough information for a specific technical solution. And it is a design question that likely belongs on Unity Forums rather than here.

avatar image FirePlantGames · May 06, 2014 at 11:46 PM 0
Share

do you want to create 1 more everytime one is destroyed? or do you want to create 1 every second or something?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by nightbane30 · May 07, 2014 at 01:11 AM

Now, there wasn't too much info that you provided us with for us to give a solid answer, but I'd say use the Instantiate method. Here's an example of it in use:

 void OnTriggerEnter (Collider collider) {
 
 if(collider.gameObject.CompareTag ("Projectile")){
 //Right now, all you have is this
 Destroy(this.gameObject);
 //If you add this, it should instantiate a new enemy prefab
 Instantiate(EnemyPrefab, EnemyPrefabPosition, Quaternion.identity);
 //the quaternion is just to show that there's now rotation
 //I used placeholder names as well.
 //Check the scripting reference for Instantiate for more info.
 }

}

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Creating scene from code 1 Answer

Rotation Speed 1 Answer

Respawn timer? Duplicate instance OnDestroy question... 0 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