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 justinpatterson · Aug 18, 2012 at 08:19 AM · objectrandomgenerationtimed

Yield after spawning object

Hey everyone,

I'm making a simple arcade-style collection game where apples fall and the player has to collect them. The apples fall from a random point (an array of empty gameObjects to simulate a tree), and then are supposed to wait a varying amount of seconds before dropping again.

I thought the code below would work, but the yield command seems to cause the method to not run (everything else runs fine). Of course, removing the yield command results in a million spheres spawning everywhere. Any suggestions? The method as I have it is supposed to spawn another apple in a time frame of 1 to 5 seconds, using Random.Range to select the specific pause length.

 function Update () { 
 makeApple();
 yield WaitForSeconds(Random.Range(1, 5));
 }
 
 function makeApple(){
 currentApple = Random.Range(0, apple.Length);
 var newApple : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
 newApple.transform.position = apple[currentApple].transform.position;
 newApple.AddComponent("Rigidbody");
 }
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
1
Best Answer

Answer by fafase · Aug 18, 2012 at 08:23 AM

You cannot use yield WaitForSeconds in the update.

That is your problem. As you call the function in the update no matter what you shuld use a timer instead. See below:

 function Update () { 
 makeApple();
 }
 
 function makeApple(){
 currentApple = Random.Range(0, apple.Length);
 var newApple : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
 newApple.transform.position = apple[currentApple].transform.position;
 newApple.AddComponent(Rigidbody);
 var wait=Random.Range(1, 5); // Note this is giving 1 to 4. Range excludes the max value when using integers
 yield WaitForSeconds(wait);
 }

This is calling the function 60 or more time per second and each call is waiting but with:

 var timer:float;
 var wait:int;
 function Start(){
   wait = Random.Range(0,5);
 }
 function Update(){
   timer+=Time.deltaTime;
   if(timer>wait){
     makeApple();
     wait = Random.Range(0,5);
     timer = 0;
   }
 }
 
 function makeApple(){
     currentApple = Random.Range(0, apple.Length);
     var newApple : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     newApple.transform.position = apple[currentApple].transform.position;
     newApple.AddComponent(Rigidbody);
     var wait=Random.Range(1, 5);
 }

now the function is not called until timer reaches the value

Comment
Add comment · Show 1 · 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 justinpatterson · Aug 18, 2012 at 01:21 PM 0
Share

Thanks! That did the trick. So, it seems that my makeApple method wasn't abstracted from the update method enough; that is, it didn't have any conditionals before running to check the time so the yield didn't yield results.

Thanks again! Now to figure out the AI >_<

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

9 People are following this question.

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

Related Questions

Tree position on random Terrain (y axis) 0 Answers

Generate an object in random positions ? 1 Answer

Random number different from previous generated. 6 Answers

Function action applies to every object that have script with that function on it. Help please 1 Answer

Make an object move towards random spot on another objects edge? 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