Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

IEnumerator and waitforseconds problems

Discussion in 'Scripting' started by LarsArni, Apr 5, 2020.

  1. LarsArni

    LarsArni

    Joined:
    Apr 3, 2020
    Posts:
    8
    Can someone tell me what I did wrong. It just doesn't execute the couroutine with WaitForSeconds, it jsut immediatly proceeds to the next thing without waiting
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         TargetSelection();
    4.     }
    5.  
    6.     IEnumerator DoingJob()
    7.     {
    8.        yield return new WaitForSeconds(20f);[
    9.     }
    10.     private void Awake()
    11.     {
    12.         //positon the person at the start point
    13.         transform.position = new Vector3(56f, 0.1f, 24f);
    14.     }
    15.     void TargetSelection()
    16.     {
    17.         StartCoroutine(DoingJob());
    18.         do
    19.         {
    20.             number = Random.Range(0, zielpositionen.Count);
    21.         }
    22.         while (number == lasttarget);
    23.         lasttarget = number;
    24.         Debug.Log("selected target: " +number);
    25.  
    26.         target = zielpositionen[number].transform;
    27.         transform.LookAt(target);
    28.  
    29.         journeyLength = Vector3.Distance(target.position, transform.position);
    30.         startTime = Time.time;
    31.         Tasks++;
    32.     }
    33.  
    34.  
    35.  
    36.    
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.  
    42.         if (Vector3.Distance(transform.position, target.position) < 1.0f)
    43.         {
    44.             Debug.Log("arrived");
    45.             TargetSelection();
    46.             return;
    47.         }
     
  2. LarsArni

    LarsArni

    Joined:
    Apr 3, 2020
    Posts:
    8
    If it helps, here's the entire class:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestMove : MonoBehaviour
    6. {
    7.  
    8.     private Transform target;
    9.     public float speed;
    10.     public int Tasks = 0;
    11.     public float waitseconds = 100f;
    12.     public int tasksinaday = 5;
    13.     public List<GameObject> zielpositionen = new List<GameObject>();
    14.     private int lasttarget;
    15.     private float journeyLength;
    16.     private float startTime;
    17.     public int number;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         TargetSelection();
    22.     }
    23.  
    24.     IEnumerator DoingJob()
    25.     {
    26.         yield return new WaitForSeconds(20f);
    27.     }
    28.     private void Awake()
    29.     {
    30.         //positon the person at the start point
    31.         transform.position = new Vector3(56f, 0.1f, 24f);
    32.     }
    33.     void TargetSelection()
    34.     {
    35.         StartCoroutine(DoingJob());
    36.         do
    37.         {
    38.             number = Random.Range(0, zielpositionen.Count);
    39.         }
    40.         while (number == lasttarget);
    41.         lasttarget = number;
    42.         Debug.Log("selected target: " +number);
    43.  
    44.         target = zielpositionen[number].transform;
    45.         transform.LookAt(target);
    46.  
    47.         journeyLength = Vector3.Distance(target.position, transform.position);
    48.         startTime = Time.time;
    49.         Tasks++;
    50.     }
    51.  
    52.  
    53.  
    54.    
    55.  
    56.     // Update is called once per frame
    57.     void Update()
    58.     {
    59.  
    60.         if (Vector3.Distance(transform.position, target.position) < 1.0f)
    61.         {
    62.             Debug.Log("arrived");
    63.             TargetSelection();
    64.             return;
    65.         }
    66.  
    67.         if (Tasks == tasksinaday)
    68.         {
    69.  
    70.         }
    71.  
    72.  
    73.  
    74.        /* for(Vector3.Distance(transform.position, target.position) > 0.5F)
    75.         {
    76.            
    77.         }*/
    78.  
    79.         // Distance moved equals elapsed time times speed..
    80.         float distCovered = (Time.time - startTime) * speed;
    81.  
    82.         // Fraction of journey completed equals current distance divided by total distance.
    83.         float fractionOfJourney = distCovered / journeyLength;
    84.  
    85.         // Set our position as a fraction of the distance between the markers.
    86.         transform.position = Vector3.Lerp(transform.position, target.position, fractionOfJourney);
    87.  
    88.  
    89.  
    90.     }
    91. }
    92.