Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with some IEnumerator

Discussion in '2D' started by Lamarqued, May 10, 2015.

  1. Lamarqued

    Lamarqued

    Joined:
    Jan 30, 2015
    Posts:
    9
    Hi everybody, it's me again !
    I'm making a video game in Unity 2D for my final exam (baccalaureate), and I have some little issues with IEnumerators.
    In fact, I'm Trying to make my player, when it collides with some Enemy, to flash like three times, but it doesn't work.
    I have this function :
    public IEnumerator takendamage(){
    renderer.enabled = false;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = true;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = false;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = true;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = false;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = true;
    yield return new WaitForSeconds(takendamage);
    }
    that I added in my Player Script.
    But it seems like enabled isn't the word, it doesn't work and when I'm trying to backup, I obtain this script :
    public IEnumerator takendamage(){
    GetComponent<Renderer>().enabled = false;
    yield return new WaitForSeconds(takendamage);
    GetComponent<Renderer>().enabled = true;
    yield return new WaitForSeconds(takendamage);
    GetComponent<Renderer>().enabled = false;
    yield return new WaitForSeconds(takendamage);
    GetComponent<Renderer>().enabled = true;
    yield return new WaitForSeconds(takendamage);
    GetComponent<Renderer>().enabled = false;
    yield return new WaitForSeconds(takendamage);
    GetComponent<Renderer>().enabled = true;
    yield return new WaitForSeconds(takendamage);
    }
    ...which is also incorrect.
    Some help pleaaaase ? :(
     
  2. alex_hajdu

    alex_hajdu

    Joined:
    Nov 10, 2013
    Posts:
    32
    There is lot of ways to do that and this is probably the most complicated. Use animation instead.
    If you want to use this, you have to keep reference to SpriteRenderer...
     
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Use StartCoroutine() to call your function, don't call it directly. You should also get familiar with loops.

    Also, use CODE tags when posting source code.
     
  4. Lamarqued

    Lamarqued

    Joined:
    Jan 30, 2015
    Posts:
    9
    I think I'll use that kind of scripting, I'm not at all in fond of animating :')
    And @blizzy , what do you mean by code tags ?
    And even by StartCoroutine function, do I have to put it out my IEnum. one ?
     
  5. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Using code tags properly

    Uhh, what??
    You're using IEnumerator and WaitForSeconds, so it looks like you are trying to run a coroutine. You use StartCoroutine() to do that.

    If that's not the problem, you should probably explain in more detail what the problem is.
     
  6. Lamarqued

    Lamarqued

    Joined:
    Jan 30, 2015
    Posts:
    9
    I've modified a bit the script, but it seems like "enabled" isn't recognized by the console...
    void Start()
    {
    StartCoroutine (takendamage);
    }
    IEnumerator takendamage(){
    renderer.enabled = false;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = true;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = false;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = true;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = false;
    yield return new WaitForSeconds(takendamage);
    renderer.enabled = true;
    yield return new WaitForSeconds(takendamage);
    }
    Don't understand, I'm pretty sorry, I'm beginning coding...
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    What is the problem, specifically? Can't find the renderer? Use gameObject.GetComponent<T>().enabled... where <T> is the component you want to get and gameObject directs it to the go that this script is on.

    Also, use code tags.