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 lilliesl · Mar 13, 2018 at 07:38 PM · spawnspawning

How can I spawn another game object once one has been destroyed?

I am currently using this script to spawn a limit of two game objects.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class newSpawn : MonoBehaviour {
 
     public Transform[] spawnPoints;      
     public GameObject enemy;
     public int enemyCount = 0;
     public int enemyLimit = 2;
 
 void Start () {
 
         startInvoke ();
     }
 
     void startInvoke()
     {
         InvokeRepeating ("spawn", 0f, 5f);
     }
         
 
     void spawn()
     {
 
         int spawnPointIndex = Random.Range (0, spawnPoints.Length); 
 
         if(enemyCount >= enemyLimit) return;
 
         Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
 
         enemyCount++;    
 
     }    
 
 }

How would I then tell the script to spawn another game object once one is destroyed, keeping the total at 2?

Any help much apprecated.

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

Answer by RetroGeek46 · Mar 13, 2018 at 07:51 PM

You can use the Update function to keep track of when an object is destroyed, and when that happens you can call the spawn function again.

I don't see you destroying the object in this script, but wherever that is happening, update the enemyCount variable over there (maybe declare it static if object is destroyed in a different script), and then use Update function to spawn a new one. It will look somewhat like this

 ...
 Destroy(gameObject);
 enemyCount--;
 ...

And then in current script you introduce a new function

 void Update() {
 if(enemyCount < enemyLimit)
 spawn();
 }

I think if you use the Update function, you might not need to use InvokeRepeating(), but that completely depends on what you are trying to achieve.

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 lilliesl · Mar 14, 2018 at 12:29 PM 0
Share

Thanks! That makes sense. However, I am having some issues getting the enemyCount variable over into the destroy game object script. How would I do that?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class kill$$anonymous$$e : $$anonymous$$onoBehaviour {
 
     void OnCollisionEnter (Collision col) 
     {
         if (col.gameObject.name == "saber") 
         {
             $$anonymous$$illEnemy();
 
         }
 
     }
 
     void $$anonymous$$illEnemy()
     {
 
         Destroy(this.gameObject);
         
             
                 
     }
 }

Thanks for your help!

avatar image RetroGeek46 lilliesl · Mar 14, 2018 at 02:22 PM 0
Share

In your newSpawn script, declare the enemyCount variable as static

 public static int enemyCount;

Then in the kill$$anonymous$$e script, use it with

 Destroy(this.gameObject);
 newSpawn.enemyCount--;


avatar image
0

Answer by IronManBK · Mar 14, 2018 at 12:51 PM

You create 2 object, and hide them, then you just have to unhide each of them by their index and time they exist appropiately.

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

78 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 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

Hide gameobject? 1 Answer

How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers

spawning game objects 1 Answer

[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers

Cannot acces other scipt 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