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 Internetman · Mar 06, 2016 at 02:40 PM · instantiatespawningspawning problemsinvokerepeatingenemies

Make a gameobject spawn before enemies spawn? (C#)

Hello! I'm developing a 2D mobile game and I have a little problem. I have created a spawn system, that spawns bouncing balls at a random location inside a rectangle. I'm now trying to create a spawn system for another gameobject, this gameobject is meant to show where the bouncing ball will spawn. It'll only show up for a second just so the player can identify and highlight that "Ahhh an enemy ball will spawn here". The Gameobject is called "ShowEnemySpawn" and I instantiate it in the same function that spawns out the enemy balls using the same position, I also use InvokeRepeating to make the bouncing balls spawn out every 2 seconds.

How do I spawn the "ShowEnemySpawn" Gameobject for 1 sec before the Bouncing enemy ball spawn?

I've been trying to search for a solution, but has been unable to find a one. So I'm really grateful for any help I can get! Here is the code!

 using UnityEngine;
 using System.Collections;
     
 public class EnemySpawn : MonoBehaviour 
 {
     
         // EnemyPrefab
         public GameObject EnemyBouncingPrefab;
     
         //Show where spawn is prefab
         public GameObject ShowEnemySpawn;
                
         // Borders
         public Transform borderTop;
         public Transform borderBottom;
         public Transform borderLeft;
         public Transform borderRight;
         
     
         void Start () 
         {
             // Spawn boucning enemies every 2 seconds, starting in 3
             InvokeRepeating("SpawnBouncingEnemy", 3, 2);
         }
     
         // Spawn a Bouncing enemy
         void SpawnBouncingEnemy() 
         {
     
                 // x position between left & right border
                 int x = (int)Random.Range (borderLeft.position.x,
                                       borderRight.position.x);
             
                 // y position between top & bottom border
                 int y = (int)Random.Range (borderBottom.position.y,
                                       borderTop.position.y);
     
                 // Sets the prefab where an enemy will spawn
                 Instantiate (ShowEnemySpawn, new Vector2 (x, y), Quaternion.identity);
     
                 // Instantiate the power up at (x, y)
                 Instantiate (EnemyBouncingPrefab,
                                     new Vector2 (x, y),
                                     Quaternion.identity); // default rotation
     
         }
     }



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

1 Reply

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

Answer by Statement · Mar 06, 2016 at 04:38 PM

You can use coroutines to easily write code that can suspend execution a second midway a function.

 using UnityEngine;
 using System.Collections;
 
 public class EnemySpawn : MonoBehaviour
 {
     // EnemyPrefab
     public GameObject EnemyBouncingPrefab;
 
     //Show where spawn is prefab
     public GameObject ShowEnemySpawn;
 
     // Borders
     public Transform borderTop;
     public Transform borderBottom;
     public Transform borderLeft;
     public Transform borderRight;
 
     void Start()
     {
         StartCoroutine(SpawnBouncingEnemy());
     }
 
     // Spawn boucning enemies every 2 seconds, starting in 3
     IEnumerator SpawnBouncingEnemy()
     {
         yield return new WaitForSeconds(3);
 
         while (true)
         {
             Vector2 spawnPoint = RandomPointWithinBorders();
 
             // Show spawn location for one second
             Object marker = Instantiate(ShowEnemySpawn, spawnPoint, Quaternion.identity);
             yield return new WaitForSeconds(1);
             Destroy(marker);
 
             // Spawn enemy
             Instantiate(EnemyBouncingPrefab, spawnPoint, Quaternion.identity);
             yield return new WaitForSeconds(1);
         }
     }
 
     Vector2 RandomPointWithinBorders()
     {
         Vector2 random = new Vector2();
         random.x = (int)Random.Range(borderLeft.position.x, borderRight.position.x);
         random.y = (int)Random.Range(borderBottom.position.y, borderTop.position.y);
         return random;
     }
 }

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 Internetman · Mar 07, 2016 at 11:13 AM 0
Share

Genius! It works! I'll definitely check out coroutines more in the future. Thank you so much for your help Statement!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to spawn multiple prefabs at different spawn rates 0 Answers

Spawn system that doesn't instantiated enemies on top of player or each other (C#) 2 Answers

Check if there's no GameObject with specific tag in the field (in the camera view) 0 Answers

Improving script which Spawns enemies up to a required number 0 Answers

How do I spawn a certain amount of objects randomly between 2 positions? 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