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
0
Question by mjr711 · Aug 10, 2013 at 06:00 PM · enemy spawnspawn points

Spawn multiple enemies from spawn point

Hello, I have a simple spawn script but when I added the var enemy2 : Game Object, it does not want to spawn enemy 2. It will only spawn enemy 1.

 var Enemy : GameObject;
 
 var Enemy2 : GameObject;
 
 var Enemy3 : GameObject;
 
 var Enemy4 : GameObject;
 
 var SpawnPoint : GameObject;
  
 //spawn enemy starting in 1 second every 5 seconds
 InvokeRepeating("SpawnEnemy", 1, 5);
  
 function SpawnEnemy()
 {
     Instantiate (Enemy, SpawnPoint.transform.position, Quaternion.identity);
 }

As you can see I have 4 different enemies to spawn but it will only spawn enemy 1. Sorry I am new to this and learning. Thank you.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cdrandin · Aug 10, 2013 at 06:03 PM

You could try var Enemies : GameObjects[] not sure of array instantiation for JS. Then just set your indexes of which you want to spawn.

You will need an update function so you know what enemy you would like to spawn, whether it be random or not.

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 mjr711 · Aug 10, 2013 at 06:23 PM 0
Share

Currently it does spawn fine, but how can I get the others to spawn at the same time? So it would be 4 enemies spawning every 5 seconds.

avatar image
0

Answer by Aevek · Aug 10, 2013 at 09:09 PM

I don't really work in javascript, but I think that for what you're asking for, something like this should work. It's not the tidiest, but it should serve to spawn 4 enemies every 5 seconds. Probably.

 function Update()
 {
     while(true)
     {
         yield WaitForSeconds(5);
         SpawnEnemies();
     }
 }

 function SpawnEnemies(){
     Instantiate (Enemy, SpawnPoint.transform.position + transform.forward, Quaternion.identity);
     Instantiate (Enemy, SpawnPoint.transform.position - transform.forward, Quaternion.identity);
     Instantiate (Enemy, SpawnPoint.transform.position + transform.right, Quaternion.identity);
     Instantiate (Enemy, SpawnPoint.transform.position - transform.right, Quaternion.identity);
 }
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
avatar image
0

Answer by mjr711 · Aug 11, 2013 at 06:06 AM

Thanks Aevek, that code was able to spawn 2 enemies but I am still unable to spawn 4.

Here is how the code is now.

var Enemy : GameObject; var Enemy2 : GameObject; var Enemy3 : GameObject; var Enemy4 : GameObject; var SpawnPoint : GameObject;

//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy", 1, 5);

function SpawnEnemy() { Instantiate (Enemy, SpawnPoint.transform.position, Quaternion.identity); }

function Update() { while(true) { yield WaitForSeconds(5); SpawnEnemies(); } }

function SpawnEnemies(){ Instantiate (Enemy, SpawnPoint.transform.position + transform.forward, Quaternion.identity); Instantiate (Enemy, SpawnPoint.transform.position - transform.forward, Quaternion.identity); Instantiate (Enemy, SpawnPoint.transform.position + transform.right, Quaternion.identity); Instantiate (Enemy, SpawnPoint.transform.position - transform.right, Quaternion.identity); }

Any ideas on what I am doing wrong? Thank you.

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

Answer by InfernoZYB · Sep 28, 2013 at 03:58 PM

Havent tested this but i think this sould work. I think the problem is you have it spawn Enemy not like enemy2 and ect here is the code:

var Enemy : GameObject;

var Enemy2 : GameObject;

var Enemy3 : GameObject;

var Enemy4 : GameObject;

var SpawnPoint : GameObject;

//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy", 1, 5);

function SpawnEnemy() { Instantiate (Enemy, SpawnPoint.transform.position, Quaternion.identity); }

//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy2", 1, 5);

function SpawnEnemy2() { Instantiate (Enemy2, SpawnPoint.transform.position, Quaternion.identity); }

//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy3", 1, 5);

function SpawnEnemy3() { Instantiate (Enemy3, SpawnPoint.transform.position, Quaternion.identity); }

//spawn enemy starting in 1 second every 5 seconds InvokeRepeating("SpawnEnemy4", 1, 5);

function SpawnEnemy4() { Instantiate (Enemy4, SpawnPoint.transform.position, Quaternion.identity); }

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

16 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

Related Questions

Trying to get enemies to stop spawning when player is destoryed 1 Answer

How do i get this code to spawn a boss after a certian amount of waves? 1 Answer

Problem with instantiate script 1 Answer

Respawning Single Enemy at a Random Range 1 Answer

How can I make sure my GameObjects don't spawn on top of each other, and that only a certain number remain in the scene at one time? 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