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 /
avatar image
0
Question by yz09tech · Feb 05, 2019 at 03:31 AM · spawning

Space Shooter: How can I spawn the asteroids more than once?

When I run Space Shooter the asteroids spawn only once. When I press the 'R' key they spawn, but only once again. How can I make them spawn multiple times in the same run?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Feb 05, 2019 at 05:45 AM

There is a nice function for this exactly

 using UnityEngine;
 using System.Collections.Generic;
 
 // Starting in 2 seconds.
 // a projectile will be launched every 0.3 seconds
 
 public class ExampleScript : MonoBehaviour
 {
     public Rigidbody projectile;
 
     void Start()
     {
         InvokeRepeating("LaunchProjectile", 2.0f, 0.3f);
     }
 
     void LaunchProjectile()
     {
         Rigidbody instance = Instantiate(projectile);
 
         instance.velocity = Random.insideUnitSphere * 5;
     }
 }

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 yz09tech · Feb 05, 2019 at 02:55 PM

Thank you for your response. I added your script to the Game Controller in the Inspector and dragged the Asteroid object to the Projectile slot in the script. It didn't work. Then, I added the script to the Main Camera instead of the Game Controller. It didn't work. What am I doing wrong?

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 Cornelis-de-Jager · Feb 05, 2019 at 09:17 PM 0
Share

I got that of the Unity page. Below is the solution of how I will personally do it.

     /* $$anonymous$$ain Variables */
     private IEnumerator spawnFunction;// keep a copy of the executing script        
     public float timeBetweenSpawn = 1; // Default it to 1 seconds
     public GameObject Astroid; // Drag a prefab from your asset folder onto here
     
     /* Temp Variables */
     public Vector3 position;
     public Quaternion rotation;
     
     void Start()
     {
         spawnFunction = SpawnAndWait();
         StartSpawn();
     }
 
     public void StopSpawn() => StopCoroutine(spawnFunction);
     public void StartSpawn () => StartCoroutine(spawnFunction);
     
     IEnumerator SpawnAndWait()
     {
         while (true)
         {
             SpawnAstroid ();
             yield return new WaitForSeconds(timeBetweenSpawn);
         }
     }
 
     // Customize the spawn function as you like
     void SpawnAstroid () {
         var astroid = Instantiate (astroid, position, rotation) as GameObject;
         /* Do $$anonymous$$ore things */
     }

this way adds flexibility to stop and start when you like. Also the functions with the => are just one liners. If they don't work replace them with actual bracket functions { }

avatar image
0

Answer by blueshark- · Feb 05, 2019 at 10:43 PM

 Instantiate(projectile, startPos.transform.position, Quaternion.identity);

You could just try and call this multiple times and it should spawn it as many times as you call it.

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

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

Related Questions

Random Spawning 1 Answer

How do i make a spawing level? 1 Answer

Remmember the spawner 2 Answers

Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.Transform' 1 Answer

Is it possible to make a physics sandbox game with coding capabilities? 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