• 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 Chickenator · Dec 29, 2014 at 10:00 AM · instantiatespawnspawning

What's wrong with my script?

Here's the script.

 #pragma strict
 
 var enemy : Transform;
 private var spawnArea : Vector2;
 private var canSpawn : boolean = true;
 
 function Update () {
 
 if (canSpawn == true) {
 
     Instantiate (enemy, spawnArea, Quaternion.identity);
     canSpawn = false;
     
     WaitForSeconds(Random.Range(0, 3));
     canSpawn = true;
 
     }
 
 }

I want it to spawn every 0-3 seconds but it ends up spawning every frame. Any help?

Thanks in advance.

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

Answer by taxvi · Dec 29, 2014 at 10:15 AM

the WaitForSeconds is a special instruction used only in co-routine functions, google for more info. it won't work in Update function. what you need is a simple timer implementation.

 #pragma strict
  
  var enemy : Transform;
  private var spawnArea : Vector2;
 
 public var spawnTime : float = 3f;
 private var timer : float = 0f;
  
  function Update () {
  
  if (timer > spawnTime) {
  
      Instantiate (enemy, spawnArea, Quaternion.identity);
      
      timer = 0f;
 
 //another note here:
 //if you don't add 'f' at the end of the numbers, unity will take them as integers
 //and return a random integer between 0, 3.
 //t$$anonymous$$s will return random float:
 
      spawnTime = Random.Range(0f, 3f);
      }
  
 timer += Time.deltaTime;
  }
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 Chickenator · Dec 29, 2014 at 10:17 AM 0
Share

This might make me sound stupid (I'm kinda new to Unity) but what's the "f" next to the numbers for? @taxvi

avatar image taxvi · Dec 29, 2014 at 10:18 AM 0
Share

it means that the number is a float, not an integer.

avatar image Chickenator · Dec 29, 2014 at 10:19 AM 0
Share

Oh xD should've read the notes. You said what it was there

avatar image taxvi · Dec 29, 2014 at 10:20 AM 0
Share

and it can take decimal values like 4.95, while integers can not

avatar image MSpiteri · Dec 29, 2014 at 10:30 AM 1
Share

@Chickenator Yes, what you said is correct. But when it comes to numbers with a decimal point, there are 3 types:

float, double, decimal

The difference is the precision (how many digits you can have in the number).

float is 32 bits

double is 64 bits

decimal is 128 bits

More info here: http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

How to Make an Expanding Spawn Area for Prefabs? 2 Answers

Instantiate is throwing my throwing my objects? 1 Answer

Having Trouble with Instantiating an object on an axis 2 Answers

How to Spawn after checking if the clones are destroyed. 1 Answer

How would I create a script that spawns objects more frequently as time goes on? 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