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 Chris12345 · Apr 16, 2013 at 01:46 PM · spawning

Stop spawning on bool false and enable on true?

Hello, how can i turn off this by using a bool to stop spawning and then make it spawn when the bool is true? i am getting no error's but it is not working? here is my code. This is C#

 using UnityEngine;
 using System.Collections;
  
 public class cointopon : MonoBehaviour
 {
  
   public Transform[] spawnPoints;
   public GameObject[] enemyPrefabs;
   public float amountEnemies = 20;  // Total number of enemies to spawn.
   public float yieldTimeMin = 0;
   public float  yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.
   public int i;
   public bool state;
   
     void Update (){
     yieldTimeMin =  PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolmin").Value;
     yieldTimeMax =     PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
     amountEnemies = PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
     state = PlayMakerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
     if (state == true){
     Spawnblocks();
     }    
     else if (state == false) {
     Debug.Log ("false");    
     }
     }
     
     
       public IEnumerator Spawnblocks() {
         
       for (i=0; i<amountEnemies; i++){
             
         var randNum = Random.Range(0, spawnPoints.Length);
         yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
  
         var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
         var pos = spawnPoints[randNum];
  
         Instantiate(obj, pos.position, pos.rotation); 
             
 }}}
Comment
Add comment · Show 17
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 geronika2004 · Apr 16, 2013 at 02:05 PM 1
Share

Spawnblocks() is enumerator, so use start coroutine, and also add Yield return null; at the end of coroutine. When false use stop coroutine

avatar image Chris12345 · Apr 16, 2013 at 02:06 PM 0
Share

Can you please show code?

avatar image geronika2004 · Apr 16, 2013 at 02:11 PM 1
Share
     using UnityEngine;
     using System.Collections;
      
     public class cointopon : $$anonymous$$onoBehaviour
     {
      
     public Transform[] spawnPoints;
     public GameObject[] enemyPrefabs;
     public float amountEnemies = 20; // Total number of enemies to spawn.
     public float yieldTime$$anonymous$$in = 0;
     public float yieldTime$$anonymous$$ax = 5; // Don't exceed this amount of time between spawning enemies randomly.
     public int i;
     public bool state;
      
     void Update (){
     yieldTime$$anonymous$$in = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinbool$$anonymous$$").Value;
     yieldTime$$anonymous$$ax = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
     amountEnemies = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
     state = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
     if (state == true){
     StartCoroutine("Spawnblocks");
     }
     else if (state == false) {
     StopCoroutine("Spawnblocks");
     Debug.Log ("false");
     }
     }
      
      
     public IEnumerator Spawnblocks() {
      
     for (i=0; i<amountEnemies; i++){
      
     var randNum = Random.Range(0, spawnPoints.Length);
     yield return new WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax)); // How long to wait before another enemy is instantiated.
      
     var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
     var pos = spawnPoints[randNum];
      
     Instantiate(obj, pos.position, pos.rotation);
     yield return null;
      
     }}}
avatar image Chris12345 · Apr 16, 2013 at 02:19 PM 0
Share

thanks it works, but i spawns to many to fast?

avatar image Polinator · Apr 16, 2013 at 03:08 PM 1
Share

Change your yieldTime$$anonymous$$in and yieldTime$$anonymous$$ax to numbers which you find more suitable.

Show more comments

1 Reply

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

Answer by powerproc_chris · Apr 16, 2013 at 03:55 PM

From geronika2004's updates to your code, I believe you would still need a flag to check if spawning is already occurring:

 private bool alreadySpawning = false;
 
 void Update() {
     ...
     //Check if should be spawning and is not already spawning
     if (state && !alreadySpawning) {
         //Start the spawn and set the flag
         StartCoroutine(Spawnblocks());
         alreadySpawning = true;
     } else if (!state && alreadySpawning) { //Check if should not be spawning and is already spawning
         //Stop the spawn and set the flag
         StopCoroutine("Spawnblocks");
         alreadySpawning = false;
     }
 }

I think this would work off the top of my head (hopefully). Any questions/comments and I will gladly respond! =)

Comment
Add comment · Show 10 · 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 Chris12345 · Apr 16, 2013 at 04:05 PM 0
Share

Assets/cointopon.cs(24,9): error CS0103: The name `start' does not exist in the current context

avatar image powerproc_chris · Apr 16, 2013 at 04:14 PM 0
Share

I edited my answer, I accidentally replaced "state" with "start" lol.. Oops!

avatar image Chris12345 · Apr 16, 2013 at 04:25 PM 0
Share

Thanks for everyone help but one more problem IndexOutOfRange exception here is my new code

     using UnityEngine;
     using System.Collections;
 
     public class cointopon : $$anonymous$$onoBehaviour
     {
 
     public Transform[] spawnPoints;
     public GameObject[] enemyPrefabs;
     public float amountEnemies = 20; // Total number of enemies to spawn.
     public float yieldTime$$anonymous$$in = 0;
     public float yieldTime$$anonymous$$ax = 5; // Don't exceed this amount of time between spawning enemies randomly.
     public int i;
     public bool state;
     private bool alreadySpawning = false;
     bool waitSpawn=false;
 
     void Update (){
     yieldTime$$anonymous$$in = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinbool$$anonymous$$").Value;
     yieldTime$$anonymous$$ax = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
     amountEnemies = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
     state = Play$$anonymous$$akerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
     //Check if should be spawning and is not already spawning
     if (state && !alreadySpawning) {
         //Start the spawn and set the flag
         StartCoroutine(Spawnblocks());
         alreadySpawning = true;
     } else if (!state && alreadySpawning) { //Check if should not be spawning and is already spawning
         //Stop the spawn and set the flag
         StopCoroutine("Spawnblocks");
         alreadySpawning = false;
     }
 }
 
 
     public IEnumerator Spawnblocks() {
     for (i=0; i<amountEnemies; i++){
 
     var randNum = Random.Range(0, spawnPoints.Length);
     yield return new WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax)); // How long to wait before another enemy is instantiated.
 
     var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
     var pos = spawnPoints[randNum];
 
     Instantiate(obj, pos.position, pos.rotation);
     waitSpawn=false;
     yield return null;
 
     }}}
avatar image powerproc_chris · Apr 16, 2013 at 04:30 PM 0
Share

What line is the error occurring on? I'm going to take a guess and say either 41, var obj = enemyPrefabs[...], or 42, var pos = spawnPoints[randNum].

avatar image Chris12345 · Apr 16, 2013 at 04:40 PM 0
Share

41 is the problem.

Show more comments

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

2D Player Spawn Issue 0 Answers

Make a game object disappear when your camera looks at it. 1 Answer

Spawning Children and Destroying them. 1 Answer

Removing gameobjects 3 seconds after spawning 3 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