- Home /
Last item not being moved (yield problem)
Hi guys,
I have turn based game where there are multiple pieces on the board, and I am moving them one at a time in a loop. First I set all the transform positions that they are going to move to, then I move player one's pieces, then set and move player two's pieces. As you can see in the code below, I have 4 loops, and every thing works except when I get to the very last piece in the very last loop. That last piece does not move. I can't figure out why. The correct transform position is set, but the the move function never seems to finish moving it there. I can tell from looking at the inspector while the game is running that the last piece has moved ever so slightly (though only enough to be visually negligible). Anyway, here is the code. Any help would be much appreciated.
function ResolveGameState(){
var tempPieceMover : GamePieceMove;
for(var i in pOneBoardPieces){
tempPieceMover = i.transform.gameObject.GetComponent(GamePieceMove);
tempPieceMover.pOneResolveGameState();
}
for(var j in pOneBoardPieces){
tempPieceMover = j.transform.gameObject.GetComponent(GamePieceMove);
tempPieceMover.Move();
}
for(var k in pTwoBoardPieces){
tempPieceMover = k.transform.gameObject.GetComponent(GamePieceMove);
tempPieceMover.pTwoResolveGameState();
}
for(var l in pTwoBoardPieces){
tempPieceMover = l.transform.gameObject.GetComponent(GamePieceMove);
Debug.Log("movePoint = " + tempPieceMover.movePoint); // was checking here to make sure things were set correctly. They were.
tempPieceMover.Move();
}
}
and the move function, which lives in the script attached to the game piece:
function Move () {
var i = 0.0;
while (i < 1.0) {
i += Time.deltaTime*0.5;
transform.position = Vector3.Lerp(transform.position, movePoint, i);
yield;
}
}
I have tried a few different things like StartCoutroutine(tempPieceMover.Move()); but nothing has worked so far. Any help would be much appreciated.
Post your functions
pOneResolveGameState(); pTwoResolveGameState();
no prob. Here you go:
function pOneResolveGameState(){
var hits : RaycastHit[] = GetHits();
var tempSpacer : SpacerBehavior;
var setSpacer : SpacerBehavior;
var tempPieceProperties : GamePieceProperties = gameObject.GetComponent(GamePieceProperties);
var tempPiecePropertiesTwo : GamePieceProperties;
for(var i in hits){
if(i.transform.tag == "Spacer"){
tempSpacer = i.transform.gameObject.GetComponent(SpacerBehavior);
var tempObject : GameObject;
if(tempSpacer.occupiedBy.Count > 0)
tempObject = tempSpacer.occupiedBy[0];
if(tempSpacer.curOccupied == true && tempObject.tag == "pOnePiece")
{}
else{
movePoint = i.transform.position - Vector3(0,1,0); //here I am setting the move point if the move is legal. I start with the closest legal move then loop to the furthest.
setSpacer = i.transform.gameObject.GetComponent(SpacerBehavior);
}
}
}
if(tempPieceProperties.hitPoints <= 0) //If a piece encounters an enemy piece
{}
else{
for(var j in hits){
if(j.transform.tag == "pTwoPiece"){
tempPiecePropertiesTwo = j.transform.gameObject.GetComponent(GamePieceProperties);
if(tempPiecePropertiesTwo.hitPoints > 0){
tempPieceProperties.hitPoints -= tempPiecePropertiesTwo.attackPoints;
tempPiecePropertiesTwo.hitPoints -= tempPieceProperties.attackPoints;
}
}
}
}
if(tempPieceProperties.hitPoints <= 0)
timeToDie = true;
else{
setSpacer.occupiedBy.Add(gameObject);
setSpacer.curOccupied = true;
}
}
function pTwoResolveGameState(){
var hits : RaycastHit[] = GetHits();
var tempSpacer : SpacerBehavior;
var setSpacer : SpacerBehavior;
var tempPieceProperties : GamePieceProperties = gameObject.GetComponent(GamePieceProperties);
var tempPiecePropertiesTwo : GamePieceProperties;
for(var i in hits){
if(i.transform.tag == "Spacer"){
tempSpacer = i.transform.gameObject.GetComponent(SpacerBehavior);
var tempObject : GameObject;
if(tempSpacer.occupiedBy.Count > 0)
tempObject = tempSpacer.occupiedBy[0];
if(tempSpacer.curOccupied == true && tempObject.tag == "pTwoPiece")
{}
else{
movePoint = i.transform.position - Vector3(0,1,0);
setSpacer = i.transform.gameObject.GetComponent(SpacerBehavior);
}
}
}
if(tempPieceProperties.hitPoints <= 0)
{}
else{
for(var j in hits){
if(j.transform.tag == "pOnePiece"){
tempPiecePropertiesTwo = j.transform.gameObject.GetComponent(GamePieceProperties);
if(tempPiecePropertiesTwo.hitPoints > 0){
tempPieceProperties.hitPoints -= tempPiecePropertiesTwo.attackPoints;
tempPiecePropertiesTwo.hitPoints -= tempPieceProperties.attackPoints;
}
}
}
}
if(tempPieceProperties.hitPoints <= 0)
timeToDie = true;
else{
setSpacer.occupiedBy.Add(gameObject);
setSpacer.curOccupied = true;
}
}
note that I don't destroy any objects at any point yet. That hasn't been implemented so far. Once I do, it will be well after the movement has taken place.
Your answer
Follow this Question
Related Questions
Yield Wait for Seconds (A little help here) 2 Answers
Need to call yield TWICE ??? (ANSWERED) 3 Answers
Respawn after delay 3 Answers
Yield WaitForSeconds doesn't work, give syntax error 1 Answer
Coroutine a function within a loop? 1 Answer