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 /
This question was closed Nov 27, 2018 at 02:39 PM by $$anonymous$$ for the following reason:

Other

avatar image
0
Question by $$anonymous$$ · Jun 24, 2015 at 04:36 AM · c#spawning

A Simple Spawning System Code.

This is what i have so far but im planing on change the code. this code help's spawn the car but it spawns at a specific time so like every 5 secs or so. Im trying to make it so the car only spawn one at a time until it despawns the next one will spawn after it.

 using UnityEngine;
 using System.Collections;
 
 public class CarController : MonoBehaviour {
 
     public GameObject enemy;
     public Vector3 spawnValues;
     public int enemyCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
     
     void Start ()
     {
         StartCoroutine (SpawnWaves ());
     }
     
     IEnumerator SpawnWaves ()
     {
         yield return new WaitForSeconds (startWait);
         while (true)
         {
             for (int i = 0; i < enemyCount; i++)
             {
                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate (enemy, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds (waveWait);
         }
     }
 }
Comment
Add comment · Show 1
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 Neilos · Jun 24, 2015 at 05:49 AM 0
Share

Rather than instantiating a new object could you just recycle the old one if you only ever want one on the screen, If you want more then just have a pool of objects that you draw from so you can have up to a maximum number of cars. You don't really need to create a new Coroutine for this either, and I$$anonymous$$HO the CarController should not spawn itself. There should be a WorldController or similar that checks the state of all your game objects in Update() and updates them accordingly (a car should know if it is alive or not). Then spawning simply becomes a matter of removing already instantiated objects from the scene and returning them to the pool only to be attached again when you want one to spawn. Does this sound like what you are trying to achieve?

1 Reply

  • Sort: 
avatar image
1

Answer by Mark Gossage · Jun 24, 2015 at 05:51 AM

Two ways you could do this:

  1. Have a static variable in the Car script which increments when a Car is Start()ed, and decrements it when the Car is Destroy()ed. Then the spawner can just look at the Car counter to decide if it wants to spawn or wait.

  2. Have only one Car & recycle it. Instead of destroying it, just call gameObject.SetEnabled(false) to make it vanish. The spawn script can just check if the Car is obj.IsActive and recycle it if it is not.

The second is better, but will only work if you want one car. If you want more than one, go to the unity life training achieve and look up Object Pooling.

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

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Spawning A Grid Of Cubes 2 Answers

Trying to make a spawning enemy code 1 Answer

How to spawn objects in areas that are only created at run time? 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