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. Dismiss Notice

Why isn't IEnumerator working?

Discussion in 'Scripting' started by Bigmancozmo, Sep 27, 2020.

  1. Bigmancozmo

    Bigmancozmo

    Joined:
    Sep 13, 2020
    Posts:
    17
    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BossControl : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         StartCouroutine(wait());
    11.     }
    12.  
    13.     IEnumerator wait(){
    14.         yield return new waitforsceconds(0.5f)
    15.         gameObject.transform.localScale = new Vector3(2,2,2);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.     }
    23. }
    24.  
    What is wrong with IEnumerator?
    Please quote me and fix my code
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Vryken likes this.
  3. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    You have to give the IENumerator a keyword like private or public beforehand, similar to a normal function.
    Code (CSharp):
    1. public IEnumerator wait(){
    2.         yield return new waitforsceconds(0.5f)
    3.         gameObject.transform.localScale = new Vector3(2,2,2);
    And StartCoRoutine takes a string value, the string being the name of the function:
    Code (CSharp):
    1. StartCouroutine("wait");
     
  4. Bigmancozmo

    Bigmancozmo

    Joined:
    Sep 13, 2020
    Posts:
    17
    Thank you.
    But why does it say it expected a semicolon after "yield return new waitforsceconds(0.5f)" and when i add it then it gives my a bunch of new errors?
     
  5. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    Because you have to have a semicolon at the end of the line.
    But you also have other issues, like:
    you have put
    waitforsceconds
    instead of
    WaitForSeconds

    you have put
    StartCouroutine
    instead of
    StartCoroutine


    Look at the errors and work through them all to sort them out. Also just telling us that you get a bunch of new errors and not saying what they are does not allow us to help you very much.
     
    Bunny83 likes this.
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @BaBiAGameStudio

    Well he didn't even read the first reply where I already pointed out that wait for seconds issue...
     
    Ch267 and BABIA_GameStudio like this.
  7. Bigmancozmo

    Bigmancozmo

    Joined:
    Sep 13, 2020
    Posts:
    17
    When I add the semicolon, it gives me 2 errors. The first one says "(12,26): error CS0246: The type or namespace name 'waitforseconds' could not be found (are you missing a using directive or an assembly reference?)"
    the second one says "(18,9): error CS0103: The name 'StartCouroutine' does not exist in the current context"

    I did manage to fix one of the errors by changing Transform to transform
     
  8. Bigmancozmo

    Bigmancozmo

    Joined:
    Sep 13, 2020
    Posts:
    17
    I did read your reply and...
    Thank you for the help i dont have any more errors thanks you!!!!
    All i had to do was experiment with my errors until they were gone
     
  9. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Not adding a "private" keyword before IEnumerator makes it automatically private. Also IEnumerator doesn't need to be public to be called, as long as it's called in the script where it was initiated.

    Not necessarily. The full name of the function or enumerator to be called followed by () also works. It's actually a better way of doing things since the IDE recognises the enumerator's name automatically.
     
  10. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Thank you. I think I'll try this next time I find myself using a coroutine.
     
  11. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Thank you for pointing out that I replied to this first thing in the morning.
     
  12. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    There is something similar about Invoke() and InvokeRepeating() where strings are used as arguments. In their case though the () are not necessary and you need to use the keyword "nameof".

    For example, invoking a method called "Shoot" after a 0.5 second delay:
    Code (CSharp):
    1.     Invoke(nameof(Shoot), .5f);
    or invoking it repeatedly every 0.5 second after a 1 second delay:
    Code (CSharp):
    1.     InvokeRepeating(nameof(Shoot), 1f, .5f);
     
    Ch267 and Munchy2007 like this.
  13. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    You'll also need to use nameof in StartCoroutine() if you need to delay it.
    Code (CSharp):
    1. StartCouroutine(nameof(Wait), delay);
     
  14. bixarrio

    bixarrio

    Joined:
    Dec 29, 2017
    Posts:
    18
    @APSchmidt you don't have to use nameof. You could just call the method
    Code (CSharp):
    1. StartCoroutine(wait(), 0.5f);
    But for the bits where you will be using a string (which is certainly available under StartCoroutine, and Invoke and InvokeRepeating require these), I absolutely advocate nameof instead of the string
     
  15. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Nope, doesn't work; that's why I added my latest post. In a delayed coroutine, you need to use either a string or nameof. :)
     
  16. bixarrio

    bixarrio

    Joined:
    Dec 29, 2017
    Posts:
    18
    Yep, that is totally my bad.

    However, I can't find a StartCoroutine that takes a second 'delay' parameter. The 'delay' in this scenario is a parameter that will get passed to the function (check the StartCoroutine signature). In that case, you'd just call it

    Code (CSharp):
    1. StartCoroutine(wait(0.5f));
    2. // or
    3. StartCoroutine(nameof(wait), 0.5f);
    In which case the wait function should take a float and do some delay on that
    Code (CSharp):
    1. IEnumerator wait(float delay)
    2. {
    3.     yield return new WaitForSeconds(delay);
    4.     // do your thing...
    5. }
    You were right, though; given what that wait is doing, the OP may as well have used
    Code (CSharp):
    1. Invoke(nameof(wait), 0.5f);
    2.  
    3. void wait()
    4. {
    5.     // do your thing...
    6. }