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 stephen_george98 · May 18, 2016 at 09:13 PM · c#coroutinespawningienumeratorcombine

Any Way to Improve These Scripts or Increase Game Performance?

Hello all,

I have these 4 scripts that spawn my obstacles in my game. They are pretty similar to the "SpawningObstacles" script in the Unity Space Shooter Tutorial. I would just like to know if there is any way I could improve these scripts in any way? Maybe combine them or make them increase game performance? I attach these scripts to my parent GameObject for the logic in my obstacles. Here they are:

Obstacle 1:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable] // Serialize this so it looks neater in the inspector 
 
 public class Obstacle1 // Hammer Obstacle 
 {
     
     public GameObject hammer; // The first obstacle gameobject. This is attached in the inspector.
     public Vector3 spawnHPosValues; // Where the first obstacle will be spawned at on the X,Y,Z plane. 
     public int hCount; // This is the count of the first obstacle in a given wave. //
     public float hSpawnWait; // Time in seconds between next wave of obstacle 1.
     public float hStartGameWait; // Time in seconds between when the game starts and when the first obstacle start spawning.
     public float hWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 1 will spawn.
 }
 
 
 public class SpawnHammers : MonoBehaviour {
 
     public Obstacle1 obstacle1; // Reference to the Obstacle 1 class //
 
     void Start () {
 
         StartCoroutine (SpawnHammer ()); // at the beginning of the game, start to function SpawnHammer() //
 
 }
 
     IEnumerator SpawnHammer () {
         
         yield return new WaitForSeconds(obstacle1.hStartGameWait);
         while (true)
         {
             
             for (int i = 0; i < obstacle1.hCount; i++) {
                 
                 Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle1.spawnHPosValues.x, obstacle1.spawnHPosValues.x), 
                                                      obstacle1.spawnHPosValues.y, 
                                                      obstacle1.spawnHPosValues.z); // I choose where I spawn the obstacles on the X,Y,Z planes. Only the X
                                                                                    // plane will be random
                 Quaternion spawnRotation = Quaternion.identity; // Spawn with NO rotation
                 Instantiate (obstacle1.hammer, spawnPosition, spawnRotation); // Bring the object into the scene
                 yield return new WaitForSeconds(obstacle1.hSpawnWait); // Spawn a new obstacle
             }
             yield return new WaitForSeconds (obstacle1.hWaveSpawnWait); // Start the new wave of obstacles
         }
     }
 }

Obstacle 4:

  using UnityEngine;
     using System.Collections;
     
     [System.Serializable]
     
     public class Obstacle4 // Barrel Obstacle
     {
         public GameObject barrel; // The fourth obstacle gameobject. This is attached in the inspector.
         public Vector3 spawnBPosValues; // Where the fourth obstacle will be spawned at on the X,Y,Z plane. 
         public int bCount; // This is the count of the fourth obstacle in a given wave.
         public float bSpawnWait; // Time in seconds between next wave of obstacle 4.
         public float bStartGameWait; // Time in seconds between when the game starts and when the fourth obstacle start spawning.
         public float bWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 4 will spawn.
     }
     
     public class SpawnBarrels : MonoBehaviour {
         
         public Obstacle4 obstacle4;
         
         void Start () {
             
             StartCoroutine (SpawnBarrel ());
         }
         
         
         IEnumerator SpawnBarrel () {
             
             yield return new WaitForSeconds(obstacle4.bStartGameWait);
             while (true)
             {
                 
                 for (int i = 0; i < obstacle4.bCount; i++) {
                     
                     Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle4.spawnBPosValues.x, obstacle4.spawnBPosValues.x), 
                                                          obstacle4.spawnBPosValues.y, 
                                                          obstacle4.spawnBPosValues.z);
                     Quaternion spawnRotation = Quaternion.Euler(0f,0f,90f); // rotate 90 degrees on the Z axis     
                     Instantiate (obstacle4.barrel, spawnPosition, spawnRotation);
                     yield return new WaitForSeconds(obstacle4.bSpawnWait);
                 }
                 yield return new WaitForSeconds (obstacle4.bWaveSpawnWait);
             }
         } 
     }

Obstacle 3:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 
 public class Obstacle3 // Cone Obstacle
 {
     public GameObject cone; // The third obstacle gameobject. This is attached in the inspector.
     public Vector3 spawnCPosValues; // Where the third obstacle will be spawned at on the X,Y,Z plane. 
     public int cCount; // This is the count of the third obstacle in a given wave.
     public float cSpawnWait; // Time in seconds between next wave of obstacle 3.
     public float cStartGameWait; // Time in seconds between when the game starts and when the third obstacle start spawning.
     public float cWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 3 will spawn.
 }
 
 public class SpawnCones : MonoBehaviour {
     
     public Obstacle3 obstacle3;
     
     void Start () {
         
         StartCoroutine (SpawnCone ());
     }
     
     
     IEnumerator SpawnCone () {
         
         yield return new WaitForSeconds(obstacle3.cStartGameWait);
         while (true)
         {
             
             for (int i = 0; i < obstacle3.cCount; i++) {
                 
                 Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle3.spawnCPosValues.x, obstacle3.spawnCPosValues.x), 
                                                      obstacle3.spawnCPosValues.y, 
                                                      obstacle3.spawnCPosValues.z);
                 Quaternion spawnRotation = Quaternion.Euler(0f,0f,90f); // rotate 90 degrees on the Z axis     
                 Instantiate (obstacle3.cone, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds(obstacle3.cSpawnWait);
             }
             yield return new WaitForSeconds (obstacle3.cWaveSpawnWait);
         }
     } 
 }

Obstacle 2:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable] 
 
 public class Obstacle2 // Road Barrier Obstacle 
 {
     
     public GameObject roadBarrier; // The second obstacle gameobject. This is attached in the inspector.
     public Vector3 spawnRBPosValues; // Where the second obstacle will be spawned at on the X,Y,Z plane. 
     public int rbCount; // This is the count of the second obstacle in a given wave. //
     public float rbSpawnWait; // Time in seconds between next wave of obstacle 2.
     public float rbStartGameWait; // Time in seconds between when the game starts and when the second obstacle will start spawning.
     public float rbWaveSpawnWait; // Time in seconds between waves when the next wave of obstacle 2 will spawn.
 }
 
 
 public class SpawnRoadBarriers : MonoBehaviour {
     
     public Obstacle2 obstacle2;
     
     void Start () {
         
         StartCoroutine (SpawnRoadBarrier ());
 
     }
     
     IEnumerator SpawnRoadBarrier () {
         
         yield return new WaitForSeconds(obstacle2.rbStartGameWait);
         while (true)
         {
             
             for (int i = 0; i < obstacle2.rbCount; i++) {
                 
                 Vector3 spawnPosition = new Vector3 (Random.Range(-obstacle2.spawnRBPosValues.x, obstacle2.spawnRBPosValues.x), 
                                                      obstacle2.spawnRBPosValues.y, 
                                                      obstacle2.spawnRBPosValues.z); 
                 Quaternion spawnRotation = Quaternion.identity; 
                 Instantiate (obstacle2.roadBarrier, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds(obstacle2.rbSpawnWait);
             }
             yield return new WaitForSeconds (obstacle2.rbWaveSpawnWait);
         }
     }
 }

I have a couple scripts I am working on that might solve what I asked here. Lemme know if you want to see them! 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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers

Unity Editor can't find IEnumerated 2 Answers

How can i change the value of a variable from an IEnumerator 0 Answers

IEnumerator instead of LateUpdate in unity 1 Answer

What is Coroutine, Yiel and IENumerator? 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