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 Killbunny.uK · Aug 21, 2012 at 03:15 PM · instantiatespawn

Spawner with max enemies

Hi, I'll try keep this as detailed as I can but short.

I'm trying to make a spawner that has max amount of enemies it can spawn say 10, I got it to the point were it will spawn 1 enemy and if he dies it spawns another but when it comes to spawning more than 1 thats were it gets annoying.

 var enemy1 : Transform;
 
 function Update () {
 var enemyTarget = GameObject.FindWithTag("Enemy");
 
 if( enemyTarget == null )
 {
   SpawnEnemy();
 }
 }
 
 function SpawnEnemy()
 {
    Instantiate(enemy1, transform.position, transform.rotation);
 }

This is the code I've got, I looked all over the internet and I tried Invoke and that didn't work, I tried While(true) but that just failed and crashed Unity o_O. So I'm at a lost with this :(.

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

Answer by gegc · Aug 21, 2012 at 03:44 PM

Right now you have an enemy instance stored in a variable, and when it is null you spawn another one. So, this code will only ever spawn one enemy at a time. There's really any number of ways you can solve this problem because it's just a programming problem, but here's one way you can do it for the sake of example:

 //This is a behaviour to stick in your enemy prefab
 public class SpawnedObject : Monobehaviour {

     public ObjectSpawner homeSpawn;
 
     void OnDestroy() {
         homeSpawn.spawnCount--;
     }
 }


 //This is the spawner
 public class ObjectSpawner : MonoBehaviour {

     //object you want to spawn
     SpawnedObject objectPrefab;
 
     //maximum allowed number of objects - set in the editor
     public int maxObjects = 1;
 
     //number of objects currently spawned
     public int spawnCount = 0;
     
     void Update() {
         if(maxObjects>spawnCount) {
             SpawnEnemy();
         }
     }
 
     void SpawnEnemy() {
         SpawnedObject obj = 
            ((GameObject)(Instantiate(objectPrefab,transform.position,transform.rotation)))
            .GetComponent<SpawnedObject>();
         obj.homeSpawn = this;
         spawnCount++;
     }
 }
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 KillbunnyuK · Aug 21, 2012 at 04:22 PM 0
Share

Thank you for the reply, for some reason both scripts keep thorwing the error "The type or namespace name `$$anonymous$$onobehaviour' could not be found". I'm not 100% why because the script names match the public class and I have the using UnityEngine and using System.Collections at the top of the page.

avatar image
0

Answer by Paulo-Henrique025 · Aug 21, 2012 at 03:48 PM

Take a look, I'm in a rush right now and can't properly explain the code but try it and see what happens when you delete an object.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SpawnTest : MonoBehaviour {
     
     public List<GameObject> spawnList;
     
     // Use this for initialization
     void Start () 
     {
         spawnList = new List<GameObject>();
         InvokeRepeating("CheckAlive", 1, 1);
         
         //Populate for the first time
         for (int i = 0; i < 10; i++)
         {
             spawnList.Add(new GameObject(Time.timeSinceLevelLoad.ToString()));
         }
     }
     
     void CheckAlive()
     {
         for (int i = 0; i < spawnList.Count; i++)
         {
             if(!spawnList[i])
             {
                 spawnList[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 spawnList[i].name = Time.timeSinceLevelLoad.ToString();
             }
         }
     }
 }
 
Comment
Add comment · Show 11 · 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 KillbunnyuK · Aug 21, 2012 at 04:25 PM 0
Share

Thank You for the reply. This works but when I try hunt for the enemies I cant find them. I looked in the scene view and I can see where they should be with the move objects tool but there is no mesh or anything of the enemy :S

avatar image Paulo-Henrique025 · Aug 21, 2012 at 06:18 PM 0
Share

Ah, I've just created a simple object, it's just a cube, you must Instantiate your enemy prefab ins$$anonymous$$d. To try my script just delete one object from the hierarchy and it will be replaced. The InvokeRepeating method calls the function that replaces dead objects every 1s, there is no need call that in the update.

avatar image KillbunnyuK · Aug 21, 2012 at 08:22 PM 0
Share

Ah, just done the same thing and it works, one thing tho when it spawns its just a normal cube ins$$anonymous$$d of a say clone of the cube prefab I done.

avatar image Paulo-Henrique025 · Aug 21, 2012 at 08:28 PM 0
Share

As I said, just make a public GameObject pointing to your prefab variable and instantiate it ins$$anonymous$$d of my simple cube creation.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class SpawnTest : $$anonymous$$onoBehaviour {

 public GameObject myEnemy;
 
     public List<GameObject> spawnList;
 
     // Use this for initialization
     void Start () 
     {
        spawnList = new List<GameObject>();
        InvokeRepeating("CheckAlive", 1, 1);
 
        //Populate for the first time
        for (int i = 0; i < 10; i++)
        {
          // Changed here AGAIN
          spawnList.Add(Instantiate(myEnemy) as GameObject);
        }
     }
 
     void CheckAlive()
     {
        for (int i = 0; i < spawnList.Count; i++)
        {
          if(!spawnList[i])
          {
           // Also changed here AGAIN
           spawnList[i] = Instantiate(myEnemy) as GameObject;
          // spawnList[i].name = Time.timeSinceLevelLoad.ToString(); //Commented so it stop changing the name
          }
        }
     }
 }
avatar image KillbunnyuK · Aug 21, 2012 at 09:05 PM 0
Share

Ah ok, I tried the code again and it keep throwing an "A namespace can only contain types and namespace declarations" error on the Public GameObject myEnemy and not sure why. Sorry to bother you with this I'm not overly sure with C#....I should learn it to be honest lol

Show more comments

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

10 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

Related Questions

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

instanstiate Spawns more then one objec 0 Answers

Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint? 1 Answer

Spawn from array 2 Answers

How to make my new Instances spawn in the right direction? 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