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 /
  • Help Room /
avatar image
0
Question by cdm89 · Feb 04, 2016 at 03:10 PM · 2d gamespawning-enemies

how to Spawning a single enemy, respawn when object not active?

Hello, I am a beginner at unity. I am trying to make an enemy spawn in my 2d game. I only want a Single Enemy to spawn, once the enemy is deactivated, a new one should spawn in its place. When the enemy reaches the collector object it should become inactive - this part works.

I can't get the spawner to spawn more enemies. I was hoping someone could tell me where I am going wrong. I think that it is because i ony initialize in awake - but i cant initalize in a fixedupdate() Thank you very much in advance any help is appreciated!

Collector Class - works fine when using active objects in the scene

 void OnTriggerEnter2D(Collider2D target)
     {
         if(target.tag == "Walking Zombie")
         {
             target.gameObject.SetActive(false);
         }
     }

Spawner class - currently not spawning

 public GameObject walkingZombie = GameObject.FindGameObjectWithTag("Walking Zombie");
 
     void Awake()
     {
         InitializeWalkingZombie();
     }
 
     void Start()
     {
 
     }
 
     void InitializeWalkingZombie()
     {
         if (walkingZombie.activeInHierarchy == false)
         {
             Instantiate(walkingZombie
                 , new Vector3(Random.Range(8.19f,8.12f)
                 , Random.Range(-6.73f,1.75f), 0), Quaternion.identity);
         }
     }




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
0
Best Answer

Answer by Taxen0 · Feb 04, 2016 at 03:26 PM

I don't see you actually calling InitializeWalkingZombie() anywhere, besides once in the start? so no more Zombies would spawn.

Is there a particular reason for not using Destroy() instead of SetActive(false)? As you are trying instantiate a new zombie object it you would end up with a lot of unused ones eventually.

Maybe a change like this would help you?

collector

 void OnTriggerEnter2D(Collider2D target)
      {
          if(target.tag == "Walking Zombie")
          {
              Destroy(target.gameObject);
              //call a method that spawns a zombie. - you need a reference to the spawner class.
              spawnerClass.InitializeWalkingZombie();
          }
      }

spawner

 void InitializeWalkingZombie()
      {
              Instantiate(walkingZombie
                  , new Vector3(Random.Range(8.19f,8.12f)
                  , Random.Range(-6.73f,1.75f), 0), Quaternion.identity);
      }
Comment
Add comment · Show 7 · 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 cdm89 · Feb 04, 2016 at 03:52 PM 0
Share

@Taxen0 Originally I tried putting the InitializeWalkingZombie inside a FixedUpdate but Unity yelled at me for it telling me i can only instantiate in a start or awake.

Perhaps I misread this- but i thought that deactivating an object was better than destroying. I only have two zombie prefabs, I figured the code would kind of juggle through them so one is active, then the other is inactive etc. Is this not a good Idea?

I tried implementing your idea, however im kind of stuck on calling the method from the spawner class in my collector class. Do I need to make an instance of the spawnerClass in the Awake() function? If you could give me some more detail on how to do that id be really greatful!

Thank you for your response by the way.

avatar image Taxen0 cdm89 · Feb 04, 2016 at 04:55 PM 0
Share

FixedUpdate() should be used for anything physics related, however the regular Update() method should have worked fine. I would not recommend having it there either though as it would have to perform the check every frame, ins$$anonymous$$d of only when it is needed (the death of a zombie?).

using SetActive to enable and disable object works as well, and in some cases it is better than using Destroy. Imagine that you have a gun that shoots many bullets, ins$$anonymous$$d of instantiating every bullet when it's fired and destroying them once they hit something we can just re-use the same bullets by setting their active state. this is commonly known as "pooling" and saves quite a bit of performance.

But if you only have 1 zombie active at one time that is nothing you need to think about as there wouldn't really be any gain.

Co$$anonymous$$g to think of it, if you only ever is going to have one Zombie you could also just relocate that one whenever you "kill" it. that could be done like this:

collector

 void OnTriggerEnter2D(Collider2D target)
       {
           ZombieScript zombie = target.GetComponent<ZombieScript>();
           zombie.Respawn();
       }

ZombieScript (you can call this whatever you want, as long as it is attached to the zombie object)

 public void Respawn(){
     transform.position = new Vector3(Random.Range(8.19f,8.12f), Random.Range(-6.73f,1.75f), 0);
     //add other stuff you might need to reset upon respawn
 }
avatar image cdm89 Taxen0 · Feb 04, 2016 at 05:17 PM 0
Share

Sorry I should have clarified this earlier. The player is just avoiding the zombie not killing it which is why i have a collector object out of the camera's view so when the zombie hits the collector it should deactivate, then when the spawner realizes there is not active zombie it should spawn a new one.

Show more comments
avatar image cdm89 · Feb 04, 2016 at 08:06 PM 0
Share

@Taxen0 just to clarify, if i use the idea mentioned above - Can I use this respawning method inside my spawning class ins$$anonymous$$d of my zombie class? - the camera moves foward so i'm not sure how id grab the coordinates, Im currently away from my comp but here is a drawing of what im trying to do alt text

unity.jpg (42.0 kB)
avatar image Taxen0 cdm89 · Feb 04, 2016 at 08:58 PM 0
Share

sure why not!

you could make something like this in the spawner class:

 private GameObject walkingZombie;
  
      void Awake() {
          walkingZombie = GameObject.FindGameObjectWithTag("Walking Zombie");
      }

     Start() {
          RepositionZombie();
      }
  
      void RepositionZombie() {
          walkingZombie.transform.position = new Vector3(Random.Range(8.19f,8.12f), Random.Range(-6.73f,1.75f), 0);
      }


The collector looks almost like before:

 void OnTriggerEnter2D(Collider2D target)
   {
       if(target.tag == "Walking Zombie")
       {
           spawnerClass.RepositionZombie();
       }
   }

you just need to get a reference to the spawnerClass inside you collector and it should work, and have one zombie anywhere in the scene at launch.

avatar image cdm89 Taxen0 · Feb 05, 2016 at 04:16 PM 0
Share

Thank you so much for all your help i really appreciate it. I was wondering if you need to reference the spawner class in a certain way? for some reason when i use a static instance, or if i use get component it does not find the method i want. - unity tells me that the object reference not set to an instance of an object...but i thought thats what my code is doing? Online I am seeing a variety of ways to do call the method, but what is really the best practice way of doing it? I dont think that my Initialize method should be public or static Spawner class

 public static WalkingZombieSpawner instance;
 
 void InitializeWalkingZombie(){
          Instantiate(walkingZombie, new Vector3(Random.Range(8.19f, 8.12f), Random.Range(-6.73f, 1.75f), 0), Quaternion.identity);
 }

Collector class

 void OnTriggerEnter2D(Collider2D target)
     {
         if(target.tag == "Walking Zombie")
         {
             target.gameObject.SetActive(false);
             WalkingZombieSpawner.instance.InitializeWalkingZombie();
 
 
         }
     }


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

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

Related Questions

Spawn game objects from array to array of transforms 0 Answers

Detect when edge of the map is in camera view and dont random spawn enemy inside of it 0 Answers

how to start over to back the frist Switch again, when the Function Spown has been done , it should be back to the function initialization but it can't. thank in andvanced 0 Answers

Obstacle appear when the player enter in a Trigger 1 Answer

2D game score 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