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 _1 · Apr 20, 2014 at 02:07 AM · c#instantiatespawninglevels

Instantiate a certain amount of prefabs

Hello their Unity developers, I am currently making a T.D/F.P.S like game where waves of enemies come at you and you have to defend yourself from them, I have a code that spawns in the enemies at a place but the problem is that it won't stop Instantiating it. I know this is because of the code being called in Update() but I want to check the level you are on. Heres my code:

   public function SpawnEnemies(enemies : int, spaceBetweenEnemies : float, EnemyForWave : Transform, SpawnPoint : Transform) : int
     {
     
     
     for(var i = 0; i < enemies; i++)
      {
      
      /*if(enemies <= enemies) this line does not work, i've also tried if(enemies >= enemies)
      {*/
      Instantiate(EnemyForWave, SpawnPoint.position, SpawnPoint.rotation);
      }
      
      //}
     
      
     }
     
     
     function Update()
     {
     
     //the problem is here
     
     if(wave == 1)
     {
     SpawnEnemies(5, 1f, RegAi, spawnArea);
     }
     
      }
 

Please help

Sorry if I maybe wasn't clear.

Thanks :)

_1

Comment
Add comment · Show 2
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 robertbu · Apr 20, 2014 at 02:18 AM 1
Share

Your problem is clear, but the solution is fuzzy because you did not give a clear description of your criteria of when SpawnEnemies() should be called. If it should be called at a regular interval, Look at InvokeRepeating(). If you want it called based on some specific criteria, then a coroutine may be what you are looking for:

http://unitygems.com/coroutines/

If it needs to only be called once per loaded level, use Start().

avatar image _1 · Apr 20, 2014 at 02:45 AM 0
Share

sorry if my description was a little unclear, SpawnEnemies() should be called when it reaches a new wave, thats why I use Update() I will try your solution and see how it works, Thanks for your time and help :)

1 Reply

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

Answer by fendorio · Apr 20, 2014 at 03:07 AM

What's happening here is that SpawnEnemies is being called every frame when the round is 1, and it's spawning 5 enemies.

I'm too tired to write up an elegant fix, so a bool trigger will suffice :P.

You need to add this somewhere:

  var trigger : bool = true; 


  public function SpawnEnemies(enemies : int, spaceBetweenEnemies : float, 
                         EnemyForWave : Transform, SpawnPoint : Transform) : int
  {

        trigger = false; //This stops the function being called in the next frame.

        for(var i = 0; i < enemies; i++)
        {
              Instantiate(EnemyForWave, SpawnPoint.position, SpawnPoint.rotation);
        }
   }

   function Update()
   {
        if(trigger == true) //The bool we just made
        {
            if(wave == 1)
             {
                 SpawnEnemies(5, 1f, RegAi, spawnArea);
             }
        }
   }

So yeah , to reiterate what's happening in your code, every frame update is called it's instantiating 5 gameObjects.

With this new code - the first frame trigger == true so SpawnEnemies is called, then at the start of SpawnEnemies we set the trigger to false, so the enemies are instantiated and on the next frame trigger == false, so the function (SpawnEnemies) isn't called.

At the end of the round or someplace you'll want to set the trigger to true so that new enemies are spawned.

As stated in a comment below your answer, this should be placed in the start function really. Just posted a fix to the code that you actually posted.

If you did it in the start function you could replace 5 (the literal you use as the argument value 'enemies' with a variable and multiply that by the level, to create more enemies each level.

 var numberOfEnemies : int = 5; // Base amount of enemies;

 function Start()
 {
      var numberOfEnemiesInWave = numberOfEnemies * wave; // 1st wave 5 enemies
                                                          // 2nd wave 10 enemies
                             //Play around with the values, i'm sure you grasp
                             //the concept..

      SpawnEnemies(numberOfEnemiesInWave, 1f, RegAi, spawnArea);
 }

Something like that :P

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 _1 · Apr 20, 2014 at 03:24 AM 0
Share

thanks it works, Thanks for helping me understand this

avatar image fendorio · Apr 20, 2014 at 03:26 AM 1
Share

No worries. Hope the project goes well :)

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

21 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

Related Questions

How to Spawn after checking if the clones are destroyed. 1 Answer

Multiple Cars not working 1 Answer

After picking up an object, spawn a new one in a random location 1 Answer

Spawning prefabs dependant upon Health UI? 2 Answers

Instantiate a prefab as a child of an existing object? 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