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

Question I need help with my IEnumerator

Discussion in 'Scripting' started by BrewinNJuicin, May 20, 2023.

  1. BrewinNJuicin

    BrewinNJuicin

    Joined:
    Feb 25, 2022
    Posts:
    11
    Hello all. I am trying to write this script where when a gameObject collides with another gameObject, it adds 3 to a variable. This is basically what I have so far:

    Code (CSharp):
    1.    
    2.  public int totalVenom = 0;
    3.     public int venomBite = 3;
    4.  
    5. private void Start()
    6.     {
    7.         StartCoroutine(AddVenom());
    8.     }
    9.  
    10.     public IEnumerator AddVenom()
    11.     {
    12.         if (_AntMovement.touchingFood == true)
    13.         {
    14.             Debug.Log("AddVenom() IEnumerator is executing");
    15.             Debug.Log("totalVenom variable: " + totalVenom);
    16.  
    17.             yield return new WaitForSeconds(3f);
    18.             totalVenom += venomBite;
    19.         }
    20.     }
    the touchingFood variable in my AntMovement script gets marked true eventually, I have debugged it a lot. Anyways, my problem here is that my script isn't working. The lines of code under the "if (_AntMovement.touchingFood == true)" do not execute. What am I doing wrong and how can I fix this? And also, is there a better way to do this? Thanks.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Did you already debug _AntMovement.touchingFood in start to see if it's actually true?
     
  3. BrewinNJuicin

    BrewinNJuicin

    Joined:
    Feb 25, 2022
    Posts:
    11
    The variable isn't supposed to be true right off the bat. I have it debugged in another script and it is eventually turned true.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    your coroutine isnt a loop at all

    after you start it, it will end instantly

    I'm not sure I recommend this, but what you want is probably:

    Code (CSharp):
    1.  
    2.  
    3. public IEnumerator AddVenom()
    4.     {
    5. while(true){
    6.         if (_AntMovement.touchingFood == true)
    7.         {
    8.             Debug.Log("AddVenom() IEnumerator is executing");
    9.             Debug.Log("totalVenom variable: " + totalVenom);
    10.  
    11.             yield return new WaitForSeconds(3f);
    12.             totalVenom += venomBite;
    13.         }
    14.  
    15. yield return null;
    16. }
    17.  
    18.  
    however the larger issue is that you never said in your post what you actually want to achieve

    if you just want to add 3 to a variable why are you trying to use a coroutine?

    why not just add 3 in the collision event? unless you want to continuously add more and more venom while staying inside, but you never said that
     
  5. BrewinNJuicin

    BrewinNJuicin

    Joined:
    Feb 25, 2022
    Posts:
    11
    Oh sorry, I forgot to mention my goal is to add 3 to the totalVenom variable every 3 seconds. Anyways, I tried your solution out, but it doesn't work. Even when the requirements are met, it doesn't run what's in the "if (_AntMovement.touchingFood == true)" if statement. I tried another solution, and it sorta works. I say this because it does add venomBite to the totalVenom variable every 3 seconds, but the totalVenom variable doesn't update for everything if that makes sense. When I log the venomBite variable, i see its value change in the console, but it doesn't update in all scripts for some reason if that makes sense. Here's what I tried:
    Code (CSharp):
    1.  
    2. //note, this is now a new script. My previous script was written in the AntManager script. This is written in the AntMovement script
    3.  private float venomTimer = 0;
    4.  public float venomCooldown = 2.0f;
    5. void Update() {
    6. Debug.Log("touchingFood variable: " + touchingFood);
    7.  
    8.         venomTimer += Time.deltaTime;
    9.  
    10.         if (touchingFood)
    11.         {
    12.             Debug.Log("venomTimer variable: " + venomTimer);
    13.  
    14.             if (venomTimer > venomCooldown)
    15.             {
    16.                 Debug.Log("totalVenom += venomBite;");
    17.                 _AntManager.totalVenom += _AntManager.venomBite;
    18.                 Debug.Log("totalVenom variable: " + _AntManager.totalVenom);
    19.                 venomTimer = 0;
    20.             }
    21.         }
    22. }
    23.  
    24. public void OnCollisionEnter2D(Collision2D collision)
    25.     {
    26.         Debug.Log("collision enter");
    27.  
    28.         if (collision.gameObject.tag == "food")
    29.         {
    30.             Debug.Log("food tag collided");
    31.             touchingFood = true;
    32.         }
    33.     }
    34.  
    What is the cause of this?
     
  6. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    well you didnt post the other scripts, but its probably because even if you make a variable with the same name in a different script it doesnt mean that its the same variable
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please use the scripting forum, not the 2D forum.

    I'll move your post.