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

Attached same script to different objects, cannot get Enumerator to run diff for each script?

Discussion in 'Scripting' started by Internetpolice, Dec 4, 2017.

  1. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    Here's what i'm trying to do in code but not exactly working....

    Code (csharp):
    1.  
    2. StartCoroutine(this.countUp());
    3.  
    4. IEnumerator countUp()
    5.     {
    6.         yield return new WaitForSeconds(Random.Range(4, 12));
    7.         Invoke("countAnchor", Time.deltaTime);
    8.     }
    9.  
    10.     void countAnchor()
    11.     {
    12.         this.currentAnchor++;
    13.         if (currentAnchor >= anchors)
    14.         {
    15.             currentAnchor = 0;
    16.         }
    17.         CancelInvoke("countAnchor");
    18.     }
    19.  
    What i'm looking to do is for each object that has the attached script.........(see below)

    I want it to generate it's own WaitForSeconds random value before incrementing its local currentAnchor value by 1....
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    You don't actually need "this" on your countUp and currentAnchor, but that shouldn't change the code behavior. Also not sure why you're running Invoke from the coroutine, but again, not a big deal.

    I also don't think CancelInvoke is going to do anything where it's at. I think it's either designed for the InvokeRepeating or to prevent an Invoke from even running (I rarely use Invoke).

    Now, that all being said, you didn't show all your code and what little you did show doesn't really tell us anything. Where are you calling your StartCoroutine from? What behavior do you see? Better to show your entire script and what you are experiencing.
     
  3. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    Hello Brathnann,


    for a start, this is the first time I've used Enumerator and coroutines but after all my digging and searching it seems to be the only way to get a random wait delay

    the coroutine call is in the update function
    and you're right, the CancelInvoke does not seem to do jack squat at the moment

    as to what i'm seeing is that currentAnchor just keeps incrementing (sometimes 50 points) during the duration of the wait time and then waits again for the next second ammount to speed through incrementing.

    What I am trying to achieve is a random wait timer for each game object this script is attached to and when the increment happens I want it to only go up by 1.

    I tried doing something like this (see below)before but it didn't do anything and threw errors saying I was returning non enumerator values.... again this is my first time trying to use an Enumerator..
    Code (csharp):
    1.  
    2.  
    3. StartCoroutine(countUp());
    4. IEnumerator countUp()
    5.    {
    6.        yield return new WaitForSeconds(Random.Range(4, 12));
    7.         currentAnchor++;
    8.         if (currentAnchor >= anchors)
    9.         {
    10.             currentAnchor = 0;
    11.         }
    12.         return;
    13.     }
    14.  
    15.  
    also sorry for not posting the code its around 850 lines long and this is my only bug
     
  4. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    is there a simple way to get an if statement to run the coroutine only one time when it hits true?
    if statement hitting math check and will continue to be set as true until coroutine returns new currentAnchor+1...
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That's your problem. You're creating a new coroutine every frame.

    You can fix this by keeping a reference to the coroutine you're currently executing.

    Code (csharp):
    1.  
    2. Coroutine counter;
    3.  
    4. void Update()
    5. {
    6.     if (counter == null)
    7.     {
    8.         counter = StartCoroutine(CountUp());
    9.     }
    10. }
    11.  
    Or by just not doing it in Update if you only need it to happen once :)
     
    Internetpolice likes this.
  6. Internetpolice

    Internetpolice

    Joined:
    Oct 16, 2017
    Posts:
    60
    I did it a bit different than this but I understood what you meant, hell-yeah! thanks for the help you gave, it works perfectly now and independent for each object as intended.
     
    Last edited: Dec 4, 2017
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Yeah, coroutines and update. That's a common error of having it trigger every frame. Glad you got it fixed!