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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Overx · Dec 11, 2012 at 03:00 PM · javascripttransform.positionwaitforseconds

Issues with randomly moving a cube

I'm trying to move a cube randomly every 2 seconds. When I press play 2 seconds passes, and then the cube starts moving every frame.

 #pragma strict
 
 var waitTime : float = 2 ;
 
 function Start () {
 
 }
 
 function Update () {
 MoveCube();
 }
 
 
 function MoveCube(){
 yield WaitForSeconds (waitTime);
 var position = Vector3(Random.Range(-6,6), Random.Range(-4,4), 0);
 transform.position = position;
 }


I'm very new to Unity so I'm sure the answer is pretty simple. Thanks

Comment
Add comment · Show 1
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 Eric5h5 · Dec 12, 2012 at 06:08 AM 0
Share

Writing "answered" in the title doesn't mark a question as answered, you need to do that by clicking the checkmark.

3 Replies

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

Answer by BiG · Dec 11, 2012 at 03:04 PM

Hi Overx, that issue happens because MoveCube() is called every frame. You can use a locking mechanism: with that, you perform MoveCube() only if the last execution is over. Just modify the script as follows:

 #pragma strict
 
 var waitTime : float = 2;
 var move_lock = false;

 function Start () {
 
 }
 
 function Update () {
 if (!move_lock)
    MoveCube();
 }
 
 
 function MoveCube(){
 move_lock = true;
 yield WaitForSeconds (waitTime);
 var position = Vector3(Random.Range(-6,6), Random.Range(-4,4), 0);
 transform.position = position;
 move_lock = false;
 }
Comment
Add comment · Show 2 · 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 Overx · Dec 12, 2012 at 02:26 AM 0
Share

Thank you! Worked out great.

avatar image BiG · Dec 13, 2012 at 12:02 PM 0
Share

@Overx, can you please mark the question as answered, as the guys suggest? You simply need to click on the green symbol at the left of my question (or at the left of the question that you find more useful). Thank you.

avatar image
0

Answer by Landern · Dec 11, 2012 at 03:07 PM

Please remember that the function Update is called every frame. You may want to use InvokeRepeating but don't call it in the update, otherwise it will have a take a repeating calls to a method/function.

With your code, yielding WaitForSeconds was called every frame(however many you get per second) which is why your cubes continued to move, while you were thinking it would wait 2 seconds and do it, but if it ran 30 frames a second, then the WaitForSeconds yield was called 30 times. :)

InvokeRepeating will only need to be called once and as the method/function name suggests, will continue repeating based off the time/repeat rate you gave it.

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
avatar image
0

Answer by Kryptos · Dec 11, 2012 at 04:03 PM

Previous answers are good. If you want to keep using coroutine, you need to start the repeating coroutine in a loop inside the Start method:

 // JS
 var someBoolean = true;
 function Start()
 {
     while( someBoolean )
     {
         yield StartCoroutine(MoveCube());
     }
 }


 // C#
 bool someBoolean = true;
 IEnumerator Start()
 {
     while( someBoolean )
     {
         yield return StartCoroutine(MoveCube());
     }
 }
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

13 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

Related Questions

Swap texture2D on enemy sprite once reached a certain point 1 Answer

How can I make a variable false for a period of time? 3 Answers

yield works, but yield WaitForSeconds does not? 1 Answer

Javascript; Wait for animation 1 Answer

yield WaitForSeconds preventing further actions. 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