• 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 Storm2141 · Sep 26, 2013 at 02:45 PM · spawncountlimitationmob

Limiting spawn number

I´m currently working on a Horrorgame were i want monsters to spawn wenn the player is inside certain collision fields, so monster randomly pop up when the player takes the wrong turn. I have already set up the spawn script wit Java, but i want to set a limitation to the amount of monster, for example no more than 10 monster should be on the map, but i dont know how to set it up correctly.

 #pragma strict
 
 var Mobs =  0;
 var Spawn = false;
 var Mob : Rigidbody;
 
 function Update () 
 {
     if (Spawn == true)
     {
     var clone : Rigidbody;
     clone = Instantiate(Mob, transform.position, transform.rotation);    
     } 
 }
 
 function OnTriggerEnter (theCollider : Collider)
     {    
         if (theCollider.tag == "Player")
         {
             Spawn = true;
         }
     }
 
     
 function OnTriggerExit (theCollider : Collider)
     {
         if (theCollider.tag == "Player")
         {
             Spawn = false;         
         }
     }
Comment
Add comment · Show 1
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 vexe · Sep 30, 2013 at 03:42 PM 0
Share

HELLO DEAR @Storm2141: I hope you're doing great! Please tick an answer if any of them helped you to keep everything tied and clean, if none, then we really appreciate your feedback to our answers letting us know so that we could improve on our answers, and improve them to meet your requirements, thanks!

2 Replies

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

Answer by vexe · Sep 26, 2013 at 03:31 PM

This shouldn't really be done this way, you should have a class to represent your mobs, but anyway, a quick and dirty patch would be to just use a counter to limit the moms.

 private var nCurrentMobs : int = 0;
 var nMaxMobs : int = 10;

each time you instantiate a mob, you first check to make sure you're below your max, and then increase the current number of mobs:

 if (nCurrentMobs < nMaxMobs)
 {
    nCurrentMobs++;
    // do your thing...
 }


A better way is to use a function to do the swapping for you, and not putting it in Update, I don't see any reason for that

 function SpawnMob()
 {
     // 1- do your checks
     // 2- instantiate
 {

 function OnTriggerEnter (theCollider : Collider) {
    if (theCollider.tag == "Player") {
      SpawnMob();
    }
 }

If you want to maintain a reference to your Mobs upon instantiation, add them to a List - Here's how. This is a lot better because if you don't keep a reference to what you instantiate, you won't be able to destroy your mob later when you're done with it.

Comment
Add comment · Show 1 · 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 vexe · Sep 26, 2013 at 03:43 PM 1
Share

Let me know how it goes, if it helps, please mark it as correct.

avatar image
0

Answer by Magarthryx · Sep 26, 2013 at 03:43 PM

1) You already catch the spawned enemy, or at least the rigidbody. Create an array in the spawner and keep track of the number of spawned enemies that way, and clean it up as monsters are removed.

2) Before a spawn is called there are a few different method for search through all of the gameobjects and looking for tags, names, or attached scripts. Set up the search to find your monsters and you have your count.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

How can record the total amount of prefabs that are spawned during runtime? 0 Answers

instantiate a set amount 3 Answers

Enemy spawner help with lengths 0 Answers

Making a simple grid 2 Answers

How do I add a timer /wait time & limiter for Spawning? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges