waitforseconds not working c#

hi!

i made a script to move a platform from point to poin, but i’d like it to stop for X seconds in each point.

the object where the script goes has an empty object as patent, the actual platform as the first child and N empty game objects that are used to set the points where the platform have to go. it works perfectly, but doen’t stops when reaches a point and goes immediately to the next.

here is the code:

`using UnityEngine;
using System.Collections;

public class Platform : MonoBehaviour {

public bool bTurnOnOff = false;
public float fSpeed = 2.0f;
public float fWaitTime = 5.0f;

private int n;

void Start () {
	n = 2;
}

void Update () {
	if (bTurnOnOff == true) {
		transform.GetChild(0).localPosition = Vector3.MoveTowards (transform.GetChild(0).localPosition, transform.GetChild(n).localPosition, fSpeed * Time.deltaTime);
		ChangePos ();
	}
}
void ChangePos () {
	if (transform.GetChild(0).localPosition == transform.GetChild(n).localPosition){
		if (n <= transform.childCount-1){
			n = n + 1;
		}
		if (n > transform.childCount-1){
			n = 1;
		}
		StartCoroutine (Wait());
	}
}
IEnumerator Wait () {
	yield return new WaitForSeconds (fWaitTime);
}

}
`

what am i doing wrong?

1 Answer

1

You’re using the coroutine wrong. You can’t cause another method to wait just because you start a coroutine that waits. You need to wait in the method that returns IEnumerator. Ex:

IEnumerator ChangePos()
{
    if (transform.GetChild(0).localPosition == transform.GetChild(n).localPosition)
        n = n <= transform.childCount-1 ? n + 1 : 1;
    yield return new WaitForSeconds(waitTime);
}

Except, in your case, you’d be calling this method potentially every frame, then waiting for something. Also, what were you waiting for? Did you intend your Update loop to not run during the wait? If that’s the case, then either don’t run your code from the Update Loop (i.e. start a coroutine in the Start or Awake method instead), or control it with a conditional.

If you explain what you’re trying to accomplish I (and others, perhaps) can help you get things working the way you want.

i'm creating a platform that will move from one point to the other (something between 2 and infinite points) to transport the player and rigid body objects, but i whant the platform to stop in each point so the player can come in easily i'll try what you sugested thank you!

–

I'll give you a hand with it when I get home tonight. Maybe someone else could offer a solution before that. Either way , what you're asking is pretty trivial. If you want to try your hand at it, this is what you need: - a list of waypoints - A script that moves the platform at a certain speed or over a given time (which do you prefer?), with a check that will detect when the platform is at the end of the line and begin reversing back over it if it doesn't loop. - A platform that childs the player to it when they enter a trigger, and unchilds it when they leave.

–

i didn't understand the line 4. could you explain? i tried EXACTLY what you posted and it doesn't work. correcting the variable "waitTime" and using your general idea with my code also did'n work

–

You mean the ternary conditional? Do you know how to use Google? Look it up! :) And the code I posted won't work for two reasons. 1) 'n' is not defined in the method. (It shouldn't even compile) 2) nothing happens a after the yield It was just an example. Also, I bet you're not calling it using the StartCoroutine method.

–