• 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
Question by ThyPanda · Nov 28, 2016 at 01:01 PM · c#instantiatespawndestroy objectenemy spawn

Respawning Single Enemy at a Random Range

Hi there,

I am quite confused at the moment, just can't seem to wrap my head around this issue. It is a game 1v1 where the player has to kill the enemy as many times as they can in an allocated time frame. The issue I am facing is how can I respawn that enemy once they are killed. With my script, it works great for the first one but after the first one, it doesn't work anymore. I Know the issue is Destroy(gameObject, 1f) but I am unaware of how to remove the enemy from the screen and spawn it back. The code is below

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour
 {
 
     public int scoreValue = 100;
     public GameObject enemyPrefab;
     bool isDead;
 
 
     void Start ()
     {
     
     }
     
     void Update ()
     {
         if (isDead == true)
         {
             Vector3 position = new Vector3(35f, Random.Range(-34.0f, 71.0f), 118f);
             Instantiate(enemyPrefab, position, Quaternion.identity);
             isDead = false;
         }
     }
 
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "Bullet")
         {
             print("Hit bullet");
             Destroy(other.gameObject);
             Death();
             Destroy(gameObject, 1f);
         }
     }
 
     void Death()
     {
         isDead = true;
         Game_Manager.score += scoreValue;
     }
 }
 
Comment

People who like this

0 Show 0
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

Answer by Kossuranta · Nov 28, 2016 at 01:43 PM

First of all you got this a little bit wrong. Currently the enemy isn't respawnin, but the script will spawn new enemy when it dies, but when it spawns the new enemy it probably doesn't have "enemyPrefab" assigned and cannot spawn new enemy. You could assign enemyPrefab at the "Assets" folder or in it's sub folder where you have prefabs and not in scene.

Or to make it "respawn" don't destroy it at all, just change its position instead of Instantiating new enemy.

But what I think you should do, is totally different script that will handle spawning while this one handles enemies death. With own script you could have it working like it does now by calling public method from that script that will spawn new enemy or you could have certain amount of enemies at the same time. Check how many enemies there are on the scene, if not enough spawn more.

Comment
Mister-Mortal

People who like this

1 Show 1 · 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 ThyPanda · Nov 28, 2016 at 11:21 PM 0
Share

When I re-read the code, I could see the issue but couldn't quite fit what was actually going wrong. The first theory I have already tried setting the prefab in the actual prefab so when it spawns it would be the enemy.

So when death animation plays out just relocate it to a new position. interesting. I will try that method and see how it flows.

The main mechanic of the game is to be a 1v1 so spawning more enemies really isn't in the books yet. But I feel like two scripts to handle different things might make a little more sense to have some flow in the game.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Spawner help 1 Answer

Spawn a power up after asteroid death 1 Answer

Tetris, Tetrominoes Clones Spawn Same Time 0 Answers

Multiple Cars not working 1 Answer

How to drag UI element immediately after instantiating/spawning it on tap/click. 4 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