Search Unity

Weird issue with transforms, use of Invoke, and NavMeshAgents

Discussion in 'Navigation' started by Mindcrime, Dec 24, 2015.

  1. Mindcrime

    Mindcrime

    Joined:
    Dec 24, 2015
    Posts:
    1
    Hi,

    I have noticed a weird issue (bug, perhaps?) when getting the 'position' of an object's transform within an Invoke() routine. Here is some sample code that demonstrates the problem:

    Code (CSharp):
    1.     void TestMe()
    2.     {
    3.         Debug.Log(transform.position);
    4.     }
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             Debug.Log(transform.position);
    12.             Invoke("TestMe", 1.8f);
    13.         }
    14.  
    15.         if (Input.GetMouseButtonDown(1))
    16.         {
    17.             Debug.Log(transform.position);
    18.             StartCoroutine("COR");
    19.         }
    20.     }
    21.  
    22.     private IEnumerator COR()
    23.     {
    24.         yield return new WaitForSeconds(1.8f);
    25.         Debug.Log(transform.position);
    26.     }
    27. }
    The output from the "TestMe" function shows a wildly different Y position for my object's transform than the output from "COR". I thought Coroutines and Invoke were similar if not the same, but there is clearly a difference here.

    However, this is only an issue when there is an active NavMeshAgent on the object. With this component disabled, the Y positions are identical.

    Is this a bug, or some weird side effect of using Invoke?

    I am on Unity 5.3.1f1. Thanks