Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Oct 03, 2015 at 01:41 PM · objectspawnwavemore

Wave spawner script

How can I spawn more than one object in my script alt text"objects to spawn"

or at least a different way to spawn more than one object in this script:

 // C#
 // WaveGenerator.cs
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 [System.Serializable]
 public class WaveAction
 {
 public string name;
 public float delay;
 public Transform prefab;
 public int spawnCount;
 public string message;
 }

 [System.Serializable]
 public class Wave
  {
 public string name;
 public List<WaveAction> actions;
  }



  public class WaveGenerator : MonoBehaviour
  {
 public float difficultyFactor = 0.9f;
 public List<Wave> waves;
 private Wave m_CurrentWave;
 public Wave CurrentWave { get {return m_CurrentWave;} }
 private float m_DelayFactor = 1.0f;
 public GameObject ObjectToSpawn; 
 
 IEnumerator SpawnLoop()
 {
     m_DelayFactor = 1.0f;
     while(true)
     {
         foreach(Wave W in waves)
         {
             m_CurrentWave = W;
             foreach(WaveAction A in W.actions)
             {
                 if(A.delay > 0)
                     yield return new WaitForSeconds(A.delay * m_DelayFactor);
                 if (A.message != "")
                 {
                     // TODO: print ingame message
                 }
                 if (A.prefab != null && A.spawnCount > 0)
                 {
                     for(int i = 0; i < A.spawnCount; i++)
                     {
                                   
                             // Random position within this transform
                             Vector3 rndPosWithin;
                             rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
                             rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
                             Instantiate(ObjectToSpawn, rndPosWithin, transform.rotation);  
                             
                     }
                 }
             }
             yield return null;  // prevents crash if all delays are 0
         }
         m_DelayFactor *= difficultyFactor;
         yield return null;  // prevents crash if all delays are 0
     }
 }
 void Start()
 {
     StartCoroutine(SpawnLoop());
 }
 

}

object.png (8.9 kB)
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 Oribow · Oct 04, 2015 at 03:53 PM 0
Share

I guess you added "ObjectToSpawn" to the script? Why did you do that? Before the script, was indeed capable of spawing different enemys!

avatar image Creative Inventory Oribow · Oct 06, 2015 at 06:05 PM 0
Share

No it wasn't!!! I had to add the "Object to spawn" there to make it spawn objects. What I want is to spawn different enemies at the same time!!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Oribow · Oct 06, 2015 at 07:33 PM

What if you change the type of "prefab" in waveaction class to "GameObject"? Then you could set the prefabs to what ever you want in the inspector. Also change your spawning, to instantiate the prefabs. Now you can

 using System.Collections;
 using System.Collections.Generic;
 [System.Serializable]
 public class WaveAction
 {
     public string name;
     public float delay;
     public GameObject prefab;
     public int spawnCount;
     public string message;
 }
 [System.Serializable]
 public class Wave
 {
     public string name;
     public List<WaveAction> actions;
 }
 public class WaveGenerator : MonoBehaviour
 {
     public float difficultyFactor = 0.9f;
     public List<Wave> waves;
     private Wave m_CurrentWave;
     public Wave CurrentWave { get { return m_CurrentWave; } }
     private float m_DelayFactor = 1.0f;
 
     IEnumerator SpawnLoop()
     {
         m_DelayFactor = 1.0f;
         while (true)
         {
             foreach (Wave W in waves)
             {
                 m_CurrentWave = W;
                 foreach (WaveAction A in W.actions)
                 {
                     if (A.delay > 0)
                         yield return new WaitForSeconds(A.delay * m_DelayFactor);
                     if (A.message != "")
                     {
                         // TODO: print ingame message
                     }
                     if (A.prefab != null && A.spawnCount > 0)
                     {
                         for (int i = 0; i < A.spawnCount; i++)
                         {
 
                             // Random position within this transform
                             Vector3 rndPosWithin;
                             rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
                             rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
                             Instantiate(A.prefab, rndPosWithin, transform.rotation);
 
                         }
                     }
                 }
                 yield return null;  // prevents crash if all delays are 0
             }
             m_DelayFactor *= difficultyFactor;
             yield return null;  // prevents crash if all delays are 0
         }
     }
     void Start()
     {
         StartCoroutine(SpawnLoop());
     }
 }
Comment
Add comment · Show 4 · 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 Creative Inventory · Oct 10, 2015 at 10:27 AM 0
Share

Thank you!

avatar image Creative Inventory · Oct 11, 2015 at 01:27 PM 0
Share

But it didn't work, thank you though

avatar image Oribow Creative Inventory · Oct 11, 2015 at 01:30 PM 0
Share

And why it didnt help? If you dont tell, I cant help you. As far as I can see it, the script should be fully capable of spawning diffrent Enemys per round.

avatar image Creative Inventory · Oct 13, 2015 at 12:04 AM 0
Share

There was an error saying something about monobehaviour is something, then when I check in the script monobehaviour was red. Sorry for my poor description but I can't remember most of it. Thank you for replying back and for your help. @ oribow

avatar image
0

Answer by moisespirela · Aug 01, 2016 at 02:50 PM

Just make an array of objects to spawn, and a for loop to go through each object in the array. Simple.

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

32 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

Related Questions

spawn bigger objects at times 1 Answer

Destroying a spawn object when it exit the main camera view not working 0 Answers

spawning problems, using array for random objects,Spawning rand prefab from array, destroy when not in the view of camera, roll new one. 0 Answers

how can I add a maximum of enemies to be generated for each wave? 0 Answers

Increase spawn object movement when player collects every 10 points 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