- Home /
[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 ;
}
}
}
}
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
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
I've BYPASSED the wait for seconds
// after 3 sec make object to kinematic so it's builded object
last code is now
if (Building == false){
if (Builded){
position = transform.position ;
time = Time.time ;
Builded = false ;
}
time1 = Time.time;
// if it's 2 seconds stationary freeze 11. building object
if (((time1 - time) > 2) && position == 13. transform.position){
transform.rigidbody.is$$anonymous$$inematic = 15. true ;
Script.NotBuilding = true ;
this.enabled = false ;
}
// if it's not stationary start at 19. beginning with new position
if ((time1 - time) > 2.3f ){
Builded = true ;
}
}
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.
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
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
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
As I said, do not use Update. You can just translate the CoUpdate Unityscript code to C#.
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