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 Geta-Ve · Jul 14, 2012 at 01:56 AM · randomspawntime.deltatimesecondsframes

Spawn Randomly over Time

Well, as simply as the title states, I am looking to spawn a certain prefab, a random number of times (lets say, 0 to 2) every second.

Thus far I am able to spawn my prefab randomly every tick via the FixedUpdate function, and I also know, I think, that I should use Time.deltaTime to work over seconds / frames, but, embarrassingly I can't figure out where or how to utilize it.

Thank you in advance for any help you guys can give!

Creation Script

 #pragma strict
 
 var newTower : Rigidbody;
 private var towerCount = 0;
 
 function Start () {}
 
 function FixedUpdate () 
 {
  var towerNum = Random.Range(0,2);
  
  for (var i = 0; i < towerNum; i++)
  {
  var towerSpawnPosition = Instantiate
  (newTower, 
  Vector3
  (
  Random.Range(-50,50),
  Random.Range(0,0),
  Random.Range(180,200)
  ), 
  Quaternion.Euler
  (
  Random.Range(0,0),
  Random.Range(0,0),
  Random.Range(0,0)
  )
  );
  
  Debug.Log("Tower has been built!");
  towerCount++;
  } 
 }
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 whydoidoit · Jul 14, 2012 at 02:09 AM

Just make Start a coroutine and do it there:

 #pragma strict
 
 var newTower : Rigidbody;
 private var towerCount = 0;
 
 function Start () {
   while(true) {
      yield new WaitForSeconds(1);
      var towerNum = Random.Range(0,2);
  
      for (var i = 0; i < towerNum; i++)
      {
        var towerSpawnPosition = Instantiate(newTower,  Vector3(Random.Range(-50,50),
            Random.Range(0,0),Random.Range(180,200)), 
            Quaternion.identity);
     Debug.Log("Tower has been built!");
      towerCount++;
   }
  } 
 }
Comment
Add comment · Show 5 · 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 Geta-Ve · Jul 14, 2012 at 02:55 AM 0
Share

This might seem like a really stupid question, but would the elements in the Start function completely replace the FixedUpdate elements? or would they both run?

I feel like you meant for me to simply add to the Start function ins$$anonymous$$d of replacing the FixedUpdate function altogether, due to your coroutine wording, but I can't be certain... (I am quite new, obviously)

Subsequently, thank you so much for taking the time to help me!

avatar image whydoidoit · Jul 14, 2012 at 03:01 AM 0
Share

Yes for several reasons:

  • You shouldn't use FixedUpdate anyway unless you are doing physics functions - it isn't necessary and can lead to poor performance.

  • You really want a coroutine which can accomplish everything you need - we could start a coroutine from Start() and that would be the right thing to do if you wanted to start and stop this behaviour later. The current example uses Start as a coroutine.

avatar image Geta-Ve · Jul 14, 2012 at 03:04 AM 0
Share

Well, I just tried your method, without the FixedUpdate function and it seems to be working brilliantly!

$$anonymous$$y hurdle now will be figuring out how to get them not to spawn at the exact same time (if I increase the random rate to, say, 0,5/10/15 etc)

Thank you so much!! Now to figure out why this all works. :)

avatar image Geta-Ve · Jul 14, 2012 at 03:15 AM 0
Share

Just saw your previous comment! :) I think I understand. I will just have to get my head around coroutines.

$$anonymous$$y goal is basically a Cube Runner style game (old iOS game) where the goal is to simply dodge onco$$anonymous$$g obstacles, in the case, towers. The difficulty will ramp up via faster moving obstacles.

This is mainly scripting learning project for me right now.

I really appreciate your help!

avatar image sid4 · Nov 05, 2016 at 01:56 PM 0
Share

hi whydo idoit" can you tell me the code for this in 2d? I tried converting but cant do it I know its something with vector 3 to 2 issue

please help

thanks

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drop item from airplane 1 Answer

random food spawning 2 Answers

AI spawning areas. What are the ways to make them? 1 Answer

Spawn object from Random Vector3 in array 0 Answers

Endless/infinite runner random enemy spawning multi lane 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