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
1
Question by Shnayzr · Sep 21, 2014 at 07:33 PM · c#invokemonster

how to: continue invoking when the first object is destroyed

Hello

i am trying to make a game that spawn monsters (using C#)

and this what i want: 1-invoke monster 2-check if the monster is dead 2a-if dead continue invoking 2b-if not dead stop invoking

i mean by "dead" gameobject is destroyed

did some codes but didnt work :(

i am using prefab to summon mobs and i tried the (if monster==null) but it doesnt work

can anyone help me please?

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

2 Replies

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

Answer by Kiwasi · Sep 21, 2014 at 07:37 PM

You don't need invoke, you need a coroutine. Simply keep a reference to the last clone and check if it has been destroyed.

 public GameObject monsterPrefab;

 IEnumerator Spawn (){
     GameObject clone;
     while (true){
         while (clone){
             yield return null;
         }
         clone = (GameObject)Instantiate(monsterPrefab);
     }
 }
Comment
Add comment · Show 8 · 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 Shnayzr · Sep 22, 2014 at 12:35 PM 0
Share

is there any other way?

cause i dont know corutine and enumerator :(

avatar image Kiwasi · Sep 22, 2014 at 07:34 PM 0
Share

There are other ways. @dmg0600 provided one solution. It can also be done with Update as follows

 public GameObject monsterPrefab;
 GameObject clone;
 
 void Update () {
     if (!clone){
         clone = (GameObject)Instantiate(monsterPrefab);
     }
 }
avatar image Kiwasi · Sep 22, 2014 at 07:34 PM 1
Share

But coroutines are really worth learning. Its one of Unity's more powerful features.

avatar image Shnayzr · Sep 22, 2014 at 08:26 PM 0
Share

thank you for your comment

but can u answer these 2 questions please?

1-is clone normal gameobject {monster} or its the object spawning {monster(clone)}

2-the if statment doesnt work cause my gameobject is a prefab and it is always not null

Thank you very much

avatar image Kiwasi · Sep 22, 2014 at 08:40 PM 0
Share

Clone is not your prefab. Clone should be the actual GameObject in the scene. Hence the clone = Instantiate line.

Note my answer is pseudo code. Its meant to demonstrate a point, you can't copy it literally.

Show more comments
avatar image
0

Answer by dmg0600 · Sep 22, 2014 at 02:04 PM

If you don't want to do it with coroutines and you don't care that the new enemy could appear after some time you can do it with InvokeRepeating:

 public GameObject monsterPrefab;
 public float respawnCheckTime = 5.0f;
 private GameObject clone;
 
 void Start()
 {
     ...
     InvokeRepeating("RespawnEnemy", 0, respawnCheckTime);
 }
 
 void RespawnEnemy()
 {
     if (clone == null)
     {
         clone = Instantiate(monsterPrefab) as GameObject;
     }
 }

This will check every respawnCheckTime if the last enemy spawned is alive, if not it will isntantiate a new one. Pretty much what @BoredMormon code does but with InvokeRepeating instead of coroutines.

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 Shnayzr · Sep 22, 2014 at 08:26 PM 0
Share

thank you for the comment

but can u answer these 2 questions please?

1-is clone normal gameobject {monster} or its the object spawning {monster(clone)}

2-the if statment doesnt work cause my gameobject is a prefab and it is always not null

Thank you very much

avatar image dmg0600 · Sep 22, 2014 at 09:48 PM 0
Share

Same answer as @Bored$$anonymous$$ormon I will also edit my answer with a variable for the prefab for future reference.

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

OnPointerClick not getting public variables from script 0 Answers

Unity was not able to run my Invoke command, and im not sure why. 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