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 sdgd · Jan 19, 2013 at 02:01 AM · coroutineupdatetimehow-toyield waitforseconds

[WORKAROUND] How to time pause in update (yield)

what I want to do is heaving a yield in middle of void update () because it's very important that the code is executed each frame

but in end I want to have infinitive loop and need yield witch doesn't work than I tried bypassing it with function and still doesn't work it just doesn't call that function

EDIT: http://www.blog.silentkraken.com/2010/01/23/coroutines-in-unity3d-c-version/

I've added the the corutine but still doesn't work in mean time when this Q was waiting alone I've figured out how to access variables in other scripts was wandering why once I could and now I cant

strange how I can access gameobject with accessing the script;

I bypassed the wait for time with 3 more if statements if anyone knows the answer how to have wait for seconds in void Update () I would be happy to hear it

what I want working(C#):

 void Start (){
     /*need for something*/
 }
 
 IEnumerator Wait3Sec (){
     yield return new WaitForSeconds(3.0f);
 }
 
 void Update () {
     while (true){
         StartCoroutine(Wait3Sec());
     }
 }


my full code (C#):

 using UnityEngine;
 using System.Collections;
 
 public class CraftPosition : MonoBehaviour {
     public bool Building = true ;
     public bool Builded = true ;
     public float Smooth = 1 ;
     public float Distance = 3 ;
     public float TimeTillBuilded = 3;
     public Create Script ;
     
     private GameObject Player ;
     private float ObjectX ;
     private float ObjectY ;
     private float ObjectZ ;
     private float time ;
     private float time1 ;
     private Vector3 position ;
     void Start () {
         Player = GameObject.Find("Camera") ;
         Script = Player.GetComponent("Create") as Create ;
 
     }
     
     
     void Update () {
         //snap position in to player's controll
         if (Building){
             Player = GameObject.Find("Camera") ;
             transform.position = Vector3.Lerp (transform.position , Player.transform.position + Player.transform.forward * Distance , Time.deltaTime * Smooth);
         
             // change craft object to gravity and snap out of player's controll
             if (Input.GetKeyDown(KeyCode.Mouse0)){
                 transform.rigidbody.useGravity = true ;
                 Building = false ;
             }
             // player's controll the distance
             if (Input.GetAxis("Mouse ScrollWheel") > 0) {Distance += 0.5f;}
             if (Input.GetAxis("Mouse ScrollWheel") < 0) {Distance -= 0.5f;}
             
             // player's controll the rotation
             if (Input.GetKey(KeyCode.Mouse1)) {
                 ObjectX = (Input.GetAxis("Mouse X"));
                 ObjectY = (Input.GetAxis("Mouse Y"));
                 if (Input.GetKey(KeyCode.Delete)){ObjectZ = 1 ;}
                 if (Input.GetKey(KeyCode.PageDown)){ObjectZ = -1 ;}
                 transform.Rotate (ObjectY,ObjectX,ObjectZ,Space.World);
             }
         }
 
 
 
         // after 3 sec make object to kinematic so it's builded object
         if (Building == false){
             if (Builded){
                 position = transform.position ;
                 time = Time.time ;
                 Builded = false ;
             }
                 time1 = Time.time;
             // if it's not stationary start at beginning with new position
             if (position != transform.position){Builded = true ;}
             // if it's X seconds stationary freeze building object
             if (((time1 - time) > TimeTillBuilded) && position == transform.position){
                 transform.rigidbody.isKinematic = true ;
                 Script.NotBuilding = true ;
                 this.enabled = false ;
             }
         }
     }
 }
Comment
Add comment · Show 3
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 sdgd · Jan 19, 2013 at 04:39 AM 0
Share

updated

from

void Start (){ /need for something/ }

IEnumerator Wait3Sec (){ yield return new WaitForSeconds(3.0f); }

void Update () { while (true){ Wait3Sec(); } }

to

void Start (){ /need for something/ }

IEnumerator Wait3Sec (){ yield return new WaitForSeconds(3.0f); }

void Update () { while (true){ StartCoroutine(Wait3Sec()); } }

but it still doesn't work

avatar image sdgd · Jan 19, 2013 at 05:52 AM 0
Share

New EDIT:

in last section I've improved the code of whole code

                 if ((time1 - time < 2) && position == transform.position){
                     transform.rigidbody.is$$anonymous$$inematic = true;
                     Script.NotBuilding = true;
                     this.enabled = false;
                 }


that's allmost everything now in this script I think

avatar image sdgd · Jan 20, 2013 at 12:19 AM 0
Share

I've BYPASSED the wait for seconds

  1. // after 3 sec make object to kinematic so it's builded object

  2. last code is now

  3. if (Building == false){

  4. if (Builded){

  5. position = transform.position ;

  6. time = Time.time ;

  7. Builded = false ;

  8. }

  9. time1 = Time.time;

  10. // if it's 2 seconds stationary freeze 11. building object

  11. if (((time1 - time) > 2) && position == 13. transform.position){

  12. transform.rigidbody.is$$anonymous$$inematic = 15. true ;

  13. Script.NotBuilding = true ;

  14. this.enabled = false ;

  15. }

  16. // if it's not stationary start at 19. beginning with new position

  17. if ((time1 - time) > 2.3f ){

  18. Builded = true ;

  19. }

  20. }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Jan 19, 2013 at 05:03 AM

Your example at the top is an infinite loop that spawns a new instance of a coroutine forever and can't possibly work on any level, so I don't know why you'd want that. I would strongly recommend that you avoid using Update entirely in this case, since it does not mix well with coroutines. From what I can make out, it sounds like you should use something like CoUpdate.

Comment
Add comment · Show 6 · 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 sdgd · Jan 19, 2013 at 05:21 AM 0
Share

do you know CoUpdate for C#?

thanks for the link tho and explaining a few

the thing is in top example that's only example and I'll propably set an if statement when to shut down this script on a object since I will never need it after as it'll have to be there where it is

avatar image sdgd · Jan 19, 2013 at 05:34 AM 0
Share

if this is what you were thinking:

void Update () {while (true)

{StartCoroutine(Wait3Sec()); } }

IEnumerator Wait3Sec ()

{yield return new WaitForFixedUpdate();}

well it doesn't work my unity freezes

avatar image sdgd · Jan 19, 2013 at 05:36 AM 0
Share

thanks for some share tho

but do you know coupdate for C#? as seems it doesn't exist in man

thing is I want to create 3 sec timer to wait after the object is being released in to the world and if it's stationary it freezes and the script never runes again on this object

avatar image Eric5h5 · Jan 19, 2013 at 05:50 AM 0
Share

As I said, do not use Update. You can just translate the CoUpdate Unityscript code to C#.

avatar image sdgd · Jan 19, 2013 at 05:55 AM 0
Share

ok how do I translate it link you provided it seems not to have translate button probably I'm wrong

http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=coupdate

and will it run each frame?

as in beginning of section it's important it works each frame for the positioning (crafting) purposes

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

10 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

Related Questions

Is there a yield WaitForSeconds type code for the update? 2 Answers

Invoke repeating OR coroutine with WaitForSeconds? 1 Answer

Movement using Time.deltaTime not working on Fast mac 3 Answers

Time Manager Class Implementation Like Coroutine in Unity 1 Answer

Coroutine in Editor? 7 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