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 NightmaresDev · Apr 24, 2017 at 02:48 PM · c#prefabspawnerenemy spawnmmorpg

Enemy Respawn After Death (MMO Style) C#

So I have looked around a lot and not really found anything that was able to help em figure out a way of spawning enemies after they were successfully destroyed with a waiting time.

I was thinking about something like World of Warcraft. Where if you kill the enemy it will take about 1-3 minutes till he would spawn back!

.

I have no script attempting and no other topic that helps me! So if you can help me figure this out or give me a Topic that has my question/problem in it, and shared it with me! that would be very kind!

Thank you 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

5 Replies

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

Answer by LoneWolfSwat · Apr 26, 2017 at 11:02 AM

Create a new C# Sharp script called "Respawn" Create a new Empty Object in the scene, called "Respawn point" Attach "Respawn" to the Respawn point Open "Respawn"

Add all this:

 public class Enemyrespawn : MonoBehaviour {
       public bool Death;
       public float Timer;
       public float Cooldown;
       public GameObject Enemy;
       public string EnemyName;
       GameObject LastEnemy;
     // Use this for initialization
     void Start () {
     //If you want, add this line:
     Death = false;
     this.gameObject.name = EnemyName + "spawn point";
     }
     
     // Update is called once per frame
     void Update () {
         if(Death == true) {
             //If my enemy is death, a timer will start.
         Timer += Time.deltaTime;
         
         }
         //If the timer is bigger than cooldown.
             if(Timer >= Cooldown) {
                 //It will create a new Enemy of the same class, at this position.
                 Enemy.transform.position = transform.position;
                 
            Instantiate(Enemy);
           LastEnemy = Find(Enemy.name + "(Clone)");
           LastEnemy.name = EnemyName;
            //My enemy won't be dead anymore.
            Death = false;
            //Timer will restart.
            Timer = 0;
             }
     }
 }

Go to your enemy health script and add:

      if(Health <= 0) {
   //Do everything you want with this part, but before destroying the enemy, add this:

      GameObject.Find(gameObject.name + ("spawn point")).GetComponent<Enemyrespawn>().Death = true;
      //Then destroy it
      Destroy(gameObject);
          
      }

----------------------------------------------------- Move your empty object to a position. (In that position your enemy will respawn)

Now, the final part. Make a prefab, and throw your enemy in there. Go to your spawner: Recommended: Death = false; Timer = 0; Cooldown = (if 3 minutes, 180 seconds) 180 Enemy = THE PREFAB, NO THE OBJECT IN THE SCENE Enemy name = Your enemy's name...

alt text


sin-titulo.png (196.2 kB)
Comment
Add comment · Show 2 · 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 NightmaresDev · Apr 27, 2017 at 07:31 AM 0
Share

The name `Find' does not exist in the current context

Even GameObject.Find doesn't work lol---

             LastEnemy = Find(Enemy.name + "(Clone)");


@LoneWolfSwat

avatar image LoneWolfSwat NightmaresDev · Apr 27, 2017 at 07:31 PM 0
Share

I'm so sorry:

 LastEnemy = GameObject.Find(Enemy.name + "(Clone)");

It's the only solution... What error does it say when you try GameObject? Have a nice day.

avatar image
0

Answer by sadowlight123 · Apr 25, 2017 at 09:33 PM

you need to follow a tutorial on any spawning items, it will help you understand it better , try going on youtube and type spwan items in unity you will get plenty of results. here are some tips you might find interesting. 1- you will need a timer 2- you will need to set spawning points and pick them randomly or set a boxlike boundaries where your enemies can spawn into 3- you will need to use the Instantiate function to create a new enemy (this function will take the gameobject prefab ,plus its position and its rotation) 4-you will need to trigger the timer to start it's countdown after the object has been destroyed and to reset and freeze that counter when the new enemy has been spawn
hope it helps have a good day :)

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
avatar image
0

Answer by Slatey · Apr 26, 2017 at 04:41 AM

steps i would use: 1. create a spawner object. it has information like what mobs can spawn, maximum number of spawns, respawn time, area mobs can spawn in. 2. spawner object runs a timer to spawn the mobs. you can have the start event spawn the maximum number if you want or a subset of that. 3. spawned mobs can be kept in a list on the spawner object. this can help in getting the count and you might employ other uses. 4. once the spawner has spawned the maximum mob count, it can switch the timer off. 5. if a mob is killed, it tells the spawner it has been killed so the spawner can start the timer again.

you could also just leave the timer on, then just check if the maximum mob count has spawned so another won't be spawned on that timer tick.

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
avatar image
0

Answer by Halwasa · Mar 21, 2018 at 12:10 AM

isent there a better way than useing update ?? because calling update to check is not a good code optimize wise. I tried using OnDestroy but I am having a trouble:

.

1) the enemy being back as the last state it was and inactive . its an Empty gameObject and I used Instantiate(Enemy,transform); // because the enemy is a child of the object so it should come out from the Transform of the parent // it did work sometimes but after cleaning the code it doesn't work and when I uncleaned it it stayed like that.

.

2) when I close the playtest I get a nullreferenceexception this is properly because the OnDestroy Get to run when the game end. I dont know how to change that so it does not be called when the game ends because I think it would cause a game break when it get built.

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
avatar image
0

Answer by prohacker001 · Feb 25, 2020 at 10:56 PM

@LoneWolfSwat how Would you do one for 2D

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Enemy prefab wont work with the enemy movement script 3 Answers

Why does this prefab trigger all instances to action? C# 1 Answer

How to generate different diagonal platforms? 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