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 /
avatar image
0
Question by jillytot · Oct 31, 2013 at 12:32 AM · timerspawninginterval

Spawn gameobject within a time window

In my project i am trying to get a gameObject to spawn randomly within a window of time. Every second, the gameObject should be spawned randomly between 0 and 1 seconds. A new game object cannot spawn until the entire second is up.

Here is my demo: http://soylentworks.com/codesucks/Builds/TouchnDrag.html You can see why i want to offset the time at which things spawn, since all 3 game objects come out at the same time.

In my code i created spawnWindow, and spawnWindowOpen to help with this, but i keep getting the wrong results. Read code comments for more detail.

Here is my current code for the Spawner object:

 using UnityEngine;
 using System.Collections;
 
 public class piSpawner : MonoBehaviour {
     
     public GameObject follower; // A single follower defined in prefab editor.
     public GameObject[] followers; // A group of followers defined in the prefab editor
     public float spawnRate = 1.0f; //The frequency at which followers spawn.
     private float nextSpawn = 0.0f; //used to trigger the next spawn
     private float nextSpawnWindow = 0.0f; // used to trigger SpawnWindow
     int followerIndex; // defines which follower to spawn
     float spawnRange; // defines the area in which the follower can spawn relative to th Spawner Object
     Vector3 spawnFromPoint; // tells the Spawner where to spawn something
     float spawnWindow; // The time interval in which a follower can spawn
     bool spawnWindowOpen = false; //a follower can only spawn if true
     
     void Start () {
         
         //this.gameObject.GetComponent<MeshRenderer>.enabled = false;
         renderer.enabled = false;
         
     }
     void Update () {
         
         followerIndex = Random.Range(0, followers.Length); // Give me a random int from followers[]
         spawnRange = Random.Range(0.5f, -0.5f);
         spawnFromPoint = new Vector3(0, spawnRange, 0);
         
         //Make followers spawn on a countdown timer
         // Every time the SpawnRate ticks, the follower can spawn at anytime between this tick,and the next tick.
         
         if (Time.time > nextSpawn && spawnWindowOpen == true) {
             
             spawnWindow = Random.Range(Time.time, Time.time + spawnRate);
             nextSpawnWindow = Time.time + spawnWindow;
             GameObject piClone = Instantiate(followers[followerIndex], transform.position + spawnFromPoint, transform.rotation) as GameObject;
             spawnWindowOpen = false;
             Debug.Log ("***Follower has been spawned***");
         }
         if (Time.time > nextSpawn && spawnWindowOpen == false) {
             nextSpawn = Time.time + spawnRate; // Trigger next spawn
             spawnWindowOpen = true;
             Debug.Log ("***Ready for the next follower***");
             
         }
     }
 }

Any help appreciated.

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
0
Best Answer

Answer by jillytot · Oct 31, 2013 at 01:47 AM

The solution ended up being a coroutine:

 using UnityEngine;
 using System.Collections;
 
 public class piSpawner : MonoBehaviour {
         
 public GameObject follower; // A single follower defined in prefab editor.
 public GameObject[] followers; // A group of followers defined in the prefab editor
 public float spawnRate = 1.0f; //The frequency at which followers spawn.
 private float nextSpawn = 0.0f; //used to trigger the next spawn
 int followerIndex; // defines which follower to spawn
 float spawnRange; // defines the area in which the follower can spawn relative to th Spawner Object
 Vector3 spawnFromPoint; // tells the Spawner where to spawn something
 bool spawnWindowOpen = false; //a follower can only spawn if true
         
     IEnumerator Start () {
                 
         //make the spawner object invisile on game start
         renderer.enabled = false;
                 
         for(;;) {
             float nextSpawnTime = Random.Range(0, spawnRate);
             yield return new WaitForSeconds(nextSpawnTime);                    
                 
             followerIndex = Random.Range(0, followers.Length); // Give me a random int from followers[]
             spawnRange = Random.Range(0.5f, -0.5f);
             spawnFromPoint = new Vector3(0, spawnRange, 0);
                 
             //Make followers spawn on a countdown timer
             // Every time the SpawnRate ticks, the follower can spawn at anytime between this tick,and the next tick.
                 
             GameObject piClone = Instantiate(followers[followerIndex], transform.position + spawnFromPoint, transform.rotation) as GameObject;
             spawnWindowOpen = false;
             Debug.Log ("***Follower has been spawned***");
             yield return new WaitForSeconds(spawnRate - nextSpawnTime);
             spawnWindowOpen = true;
             Debug.Log ("***Ready for the next follower***");
         }
     }
 }
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

15 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

Related Questions

Calculating interval dependants; ie: Velocity 2 Answers

C# Timer and Spawning Prefabs - 2d Sidescroller 1 Answer

How to stop spawning enemies after countdown timer reaches zero? 1 Answer

Increasing spawn rate over time 1 Answer

Timer-function which calculates how long an enabled script is running yet? 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