Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PlotDevice · Dec 03, 2013 at 01:02 PM · coroutineyield

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Dec 03, 2013 at 01:19 PM 0
Share

Post your functions

pOneResolveGameState(); pTwoResolveGameState();

avatar image PlotDevice · Dec 03, 2013 at 04:50 PM 0
Share

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;
         }
 }
avatar image PlotDevice · Dec 03, 2013 at 04:52 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges