Hi,
I have a small problem with my code. Function 1 on script 1 is supposed to start function 2 on script two.
IEnumerator moveUnit()
{
if (Map.validMovementGameObjects.Contains(this))
{
//Generate Pathfinding for Selected Unit to this Tile
Map.GeneratePathTo(TileX, TileZ);
// reset all tiles previously shown as selected
battleSystems.resetAllTilesSelection();
//Make Unit move to the next Tile
selectedUnit.StartCoroutine("MoveNextTile");
yield return selectedUnit.MoveNextTile();
Debug.Log("is animation still running?");
//TargetTile is now occupied by the selected unit
currentlyOccupiedby = selectedUnit;
//get current selected Tile, reset last
battleSystems.OccupiedTiles();
if (selectedUnit.movementPointsLeft >= 1)
{
Map.GenerateValidMovementTiles();
}
else
{
Map.GenerateValidAttackableTiles();
}
}
else
{
resetSelection();
}
yield break;
}
Then wait until the function has finished and then resume.
Code 2 / function, that has to finish:
public IEnumerator MoveNextTile()
{
while (movementPointsLeft > 0)
{
if (CurrentPath == null)
{
yield break;
}
// Get cost from current tile to next tile
movementPointsLeft -= Map.CostToEnterTile(CurrentPath[0].x, CurrentPath[0].z, CurrentPath[1].x, CurrentPath[1].z);
// Move us to the next tile in the sequence
setTileX = CurrentPath[1].x;
setTileZ = CurrentPath[1].z;
Maptile _targetTile = battleSystems.GetMaptile(getTileX, getTileZ);
StartCoroutine("TurnShip", _targetTile);
yield return TurnShip(_targetTile);
//Debug.Log("here the CurrentPath went missing " + CurrentPath.Count);
while (Vector3.Distance(transform.position, _targetTile.transform.position) > 0.5f)
{
// Debug.Log("Distance " + Vector3.Distance(transform.position, _targetTile.transform.position));
transform.position = Vector3.Lerp(transform.position, _targetTile.transform.position, lerpSmoothing * Time.deltaTime);
yield return null;
}
transform.position = _targetTile.transform.position; // Update our unity world position
//Debug.Log("here the CurrentPath went missing " + CurrentPath.Count);
// Remove the old "current" tile
CurrentPath.RemoveAt(0);
if (CurrentPath.Count == 1)
{
// We only have one tile left in the path, and that tile MUST be our ultimate
// destination -- and we are standing on it!
// So let's just clear our pathfinding info.
CurrentPath = null;
}
yield return null;
}
}
my problem is, that when I use:
yield return selectedUnit.MoveNextTile();
to wait for the other function to finish some of the values in code 2 change drasticly.
For example the list:
CurrentPath is suddenly nonexistent.
Alse the value for movementPointsLeft -= Map.CostToEnterTile(CurrentPath[0].x, CurrentPath[0].z, CurrentPath[1].x, CurrentPath[1].z); is suddenly way of.
Thank you for your help.