Search Unity

Variable Changing Immediatley Upon Being Passed Into IEnumerator Coroutine

Discussion in 'Scripting' started by csmaster10102, Sep 13, 2019.

  1. csmaster10102

    csmaster10102

    Joined:
    Jul 22, 2019
    Posts:
    5
    I have this code:
    Code (CSharp):
    1. protected IEnumerator moveToTargetLocation(Vector3 destination, float minDistFromDestination) {
    2.      
    3.        //TODO figure out why this prints as 0
    4.         if(this is FemaleHero) {
    5.             Debug.Log("Mindist: " + minDistFromDestination);
    6.         }
    7.    
    8.         moving = true;
    9.        
    10.    
    11.         while(moving) {
    12.             transform.position += (destination - transform.position) * slideSpeed * Time.deltaTime;
    13.        
    14.             if( Vector2.Distance(transform.position, destination) < minDistFromDestination) {
    15.                 Debug.Log("executed");
    16.                 moving = false;
    17.             }
    18.             yield return null;
    19.         }
    20.     }
    21.  
    22.     public void returnToOriginalPosition() {
    23.         StartCoroutine(moveToTargetLocation(origin, 1.0f));
    24.     }
    When I start my coroutine in returnToOriginalPosition() and pass in my 1.0f, the 1.0f becomes 0 for seemingly no reason. Literally the first thing I do in the coroutine is print the value and it changes to 0. I am absolutely confounded. Does anyone know why this happens? If you need to see more code I will be happy to provide it but I left out everything that I feel is irrelevant.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    26
    It is not possible for the parameter value to change from 1 to 0 spontaneously in the above example. The problem must be wherever you're calling that method, that you are actually passing in the value of 0.

    Are you sure that console message originates from the returnToOriginalPosition method and not somewhere else?
     
    Last edited: Sep 14, 2019
  3. csmaster10102

    csmaster10102

    Joined:
    Jul 22, 2019
    Posts:
    5
    Yes I am sure of that
     
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    216
    Do you call StartCoroutine(moveToTargetLocation(origin, 0)) anywhere?

    You can try removing minDistFromDestination and using a field on your script instead, it will work better for you.
     
  5. csmaster10102

    csmaster10102

    Joined:
    Jul 22, 2019
    Posts:
    5
    I tried using a field. I also tried using a class to wrap both variables into one object so I can just pass the one object into the function. I get the same result. I do call StartCoroutine(moveToTargetLocation(dest, n)) elsewhere but it only causes a problem here.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    Check the stacktrace (below the Debug.Log message in the console) to see where the call is actually coming from when the value is 0. I bet it's not where you expect.

    In any case, it's definitely 0 before it gets into that function. If you post the code that calls that function we could help you work out why.
     
  7. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    216
    When you changed to using a field, you can use the editor to see everywhere the field is written to, and what its default value is. This will make clear how you are writing to the field. You can also make the field private and attribute it [NonSerialized] to avoid issues with your scene.

    You will have to try removing the elsewhere. It'll become clear to you what is wrong.