- Home /
How do i use yield WaitForSeconds(); in this script?
The script is pretty long:
#pragma strict
var steps : Transform[];
var stepsNum : int;
function Start () {
}
function Update () {
stepsNum = gameObject.Find("DiceMug").GetComponent(Dice).player1steps;
if(stepsNum == 1) {
transform.position = steps[1].position;
}
if(stepsNum == 2) {
transform.position = steps[2].position;
}
if(stepsNum == 3) {
transform.position = steps[3].position;
}
if(stepsNum == 4) {
transform.position = steps[4].position;
}
if(stepsNum == 5) {
transform.position = steps[5].position;
}
if(stepsNum == 6) {
transform.position = steps[6].position;
}
if(stepsNum == 7) {
transform.position = steps[7].position;
}
if(stepsNum == 8) {
transform.position = steps[8].position;
}
if(stepsNum == 9) {
transform.position = steps[9].position;
}
if(stepsNum == 10) {
transform.position = steps[10].position;
}
if(stepsNum == 11) {
transform.position = steps[11].position;
}
if(stepsNum == 12) {
transform.position = steps[12].position;
}
if(stepsNum == 13) {
transform.position = steps[13].position;
}
if(stepsNum == 14) {
transform.position = steps[14].position;
}
if(stepsNum == 15) {
transform.position = steps[15].position;
}
if(stepsNum == 16) {
transform.position = steps[16].position;
}
if(stepsNum == 17) {
transform.position = steps[17].position;
}
if(stepsNum == 18) {
transform.position = steps[18].position;
}
if(stepsNum == 19) {
transform.position = steps[19].position;
}
if(stepsNum == 20) {
transform.position = steps[20].position;
}
if(stepsNum == 21) {
transform.position = steps[21].position;
}
if(stepsNum == 22) {
transform.position = steps[22].position;
}
if(stepsNum == 23) {
transform.position = steps[23].position;
}
if(stepsNum == 24) {
transform.position = steps[24].position;
}
if(stepsNum == 25) {
transform.position = steps[25].position;
}
if(stepsNum == 26) {
transform.position = steps[26].position;
}
if(stepsNum == 27) {
transform.position = steps[27].position;
}
if(stepsNum == 28) {
transform.position = steps[28].position;
}
if(stepsNum == 29) {
transform.position = steps[29].position;
}
if(stepsNum == 30) {
transform.position = steps[30].position;
}
if(stepsNum == 31) {
transform.position = steps[31].position;
}
if(stepsNum == 32) {
transform.position = steps[32].position;
}
if(stepsNum == 33) {
transform.position = steps[33].position;
}
if(stepsNum == 34) {
transform.position = steps[34].position;
}
if(stepsNum == 35) {
transform.position = steps[35].position;
}
if(stepsNum == 36) {
transform.position = steps[36].position;
}
if(stepsNum == 37) {
transform.position = steps[37].position;
}
if(stepsNum == 38) {
transform.position = steps[38].position;
}
if(stepsNum == 39) {
transform.position = steps[39].position;
}
if(stepsNum == 40) {
transform.position = steps[40].position;
}
if(stepsNum == 41) {
transform.position = steps[41].position;
}
if(stepsNum == 42) {
transform.position = steps[42].position;
}
What i want is for every: if(stepsNum == number) { transform.position = steps[number].position;
}
it is like this: if(stepsNum == number) { yield WaitForSeconds(somenumber); transform.position = steps[number].position;
}
But as you probably know, you cant use waitforseconds in function Update.
How can i make this happen in a other way?
}
Answer by OrbitGames · Jun 17, 2014 at 03:07 PM
To shorten your code you could either put it all in a for loop
for (int i = 1; i < 42; i ++) { if (stepsNum == i) { transform.position = steps[i].position; } }
or just use
transform.position = steps[stepsNum].position;
sorry for the unwanted bit.
I would make a counter which you subtract Time.deltaTime from every Update.
If the timer is smaller than zero (!important: Timer < 0 not Timer == 0) execute your position change.
Im not very good at loops, can you explain a little better?
Your answer
![](https://koobas.hobune.stream/wayback/20220613151753im_/https://answers.unity.com/themes/thub/images/avi.jpg)