• 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 rabione · Jan 15, 2017 at 09:44 PM · instantiate prefab

limit number of gameobjects instantiated

Hello all!!

Sorry for my another stupid question...

Also following the Survival Shooter Tutorial, I'm using the enemyManager script.

It spawns clones (from prefab) as I want. But I do want to limit their number (ressources issues)

How can I do that?

Here's the full code :



using UnityEngine;

public class EnemyManager : MonoBehaviour { public PlayerHealth playerHealth;
public GameObject enemy;
public float spawnTime = 3f;
public Transform[] spawnPoints;

 void Start ()
 {
     
     InvokeRepeating ("Spawn", spawnTime, spawnTime);
 }


 void Spawn ()
 {
     
     if(playerHealth.currentHealth <= 0f)
     {
         
         return;
     }


     int spawnPointIndex = Random.Range (0, spawnPoints.Length);


     Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
 }

}

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

Answer by KoenigX3 · Jan 15, 2017 at 10:32 PM

 int maxEnemy = 50;
 int enemyCount = 0;
 
 void Spawn()
 {
     if(enemyCount >= maxEnemy) return;
     // Instantiate
     enemyCount++;
 }

That is quite an easy approach. If you are going to kill the enemies, you should decrease the enemyCount, and the method will create a new one.

However, if you are not going to do much with them, you should probably cancel the invoke by calling

 CancelInvoke("Spawn");

and restart the invoke when you need it. This thing is for performance-efficiency.

Comment
Add comment · Show 3 · 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 rabione · Jan 16, 2017 at 12:04 AM 0
Share

Nice script!!

I've tryed to make a few enemies and decreased enemyCount parameter; but it don't creat new enemies when I kill those instantiated :

void Start () {

     InvokeRepeating ("Spawn", spawnTime, spawnTime);
 }


 void Spawn ()
 {
     
     if(playerHealth.currentHealth <= 0f)
     {
         
         return;
     }


     int spawnPointIndex = Random.Range (0, spawnPoints.Length);

     if(enemyCount >= maxEnemy) 

         return;
     
     Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

     enemyCount++;
 }
avatar image KoenigX3 rabione · Jan 16, 2017 at 10:25 AM 0
Share

First, you have to make a public function in the Enemy$$anonymous$$anager class. You must call it every time an enemy gets killed. To access the script, you must find the GameObject this script is attached to, then access the script by

 managerGameObject.GetComponent<Enemy$$anonymous$$anager>().Function();

where managerGameObject is the GameObject this script is attached to, and Function() is the new function, which you should call every time an enemy gets killed. In this function you should decrease the enemyCount. If you are cancelling the invoke somewhen, you should check if you need to restart it.

Also, finding the right gameobject at runtime can have an impact on the performance. You could create an Enemy$$anonymous$$anager-typed variable in the enemy script, and initialize that when instantiating.

 GameObject enemy = (GameObject) Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
 enemy.GetComponent<EnemyScript>().manager = this;

This will create a permanent reference to the Enemy$$anonymous$$anager instance in every enemy object. When you kill an enemy, and you must call the function in the Enemy$$anonymous$$anager, you don't need to find the GameObject - you can use the 'manager' variable to call the function.

avatar image rabione KoenigX3 · Jan 16, 2017 at 09:38 PM 0
Share

Thank you so much :)

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

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

Related Questions

Only allowing one instance of prefab created by button press & destroying prefab on collision? 0 Answers

instantiated projectile conflicts 0 Answers

How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers

Making a time bomb 1 Answer

Problems with instantiated objects in a circle formation 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