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
-1
Question by Mike 4 · Jul 24, 2010 at 02:03 AM · coroutineyieldtranslation

yield troubles. function needs to finish before continuing

Ok so I'm not to sure how to explain this.

I am using the MoveObject script from the wiki. Its awesome, It does waht I need it to do just fine. The problem I have is the function that I am calling MoveObject->Translation needs to finish before moving on.

Here is Where I call my moveFunction

HexManager.GetInstance().MovePlayer( newPos );

 activeUnit.canMove = false; 
 StateMachine.GetInstance().PopState();      

Now this is that function

function MovePlayer( destPos : Vector2 )

{ // turn off any grid HexManager.GetInstance().TurnOffCurrentGrid();

var endHex; // Get the active unit var activeUnit = WorldManager.GetInstance().GetActiveUnit();

// Get the script component Unit from the active unit var activePlayerScript = activeUnit.GetComponent( "Unit" );

Debug.Log( " BEFORE activeUnit pos = " + activePlayerScript.hexPos );
// Search for shortest path AStar.GetInstance().Search( activePlayerScript.hexPos, destPos );

// If path found move player if( AStar.GetInstance().IsPathFound() == true ) { // reset current hex variables GetHex( activePlayerScript.hexPos ).GetComponent( HexData ).hexOccupant = null; GetHex( activePlayerScript.hexPos ).GetComponent( HexData ).isOccupied = false;

 // Get the path
 var highlightPath = AStar.GetInstance().GetPath();      

 if(  highlightPath.length - 1 <= activePlayerScript.moveRange )
 {           
     var moving : boolean = false;

     var mainCam = GameObject.Find( "Main Camera" ).GetComponent( FreeFollowCamera );
     mainCam.SetUserControl( false );
     mainCam.SetFollowTarget( activeUnit );              

     // translate between each hex in path
     for( index = 0; index < highlightPath.length; ++index )
     {   
         if( highlightPath[ index ].GetComponent( HexData ).IsTrapped() == true )
         {
             yield MoveObject.GetInstance().Translation( activeUnit.transform , activeUnit.transform.position, highlightPath[ index ].transform.position, 10, 1 );

             endHex = highlightPath[ index ].GetComponent( HexData );
             endHex.trap.GetComponent( Trap ).Trigger();

             // Change units position 
             activePlayerScript.SetHexPos( endHex.hexPos );              

             break;
         }
         else
         {
             yield MoveObject.GetInstance().Translation( activeUnit.transform , activeUnit.transform.position, highlightPath[ index ].transform.position, 10, 1 );               
             endHex = highlightPath[ index ].GetComponent( HexData );

             // Change units position 
             activePlayerScript.SetHexPos( endHex.hexPos );              
         }
     }           
 }
 else
 {
     // A Pop up box will or sound or text will notify the player they can't move there for what ever reason?

     // Temp 
     print( "Dest pos = " + destPos );
     print( " Cannot move there " );
     return;
 }

} else { print( " Path not found" ); return; }

// Update hex GetHex( endHex.hexPos ).GetComponent( HexData ).hexOccupant = activeUnit; GetHex( endHex.hexPos ).GetComponent( HexData ).isOccupied = true;

Debug.Log( " AFTER activeUnit pos = " + activePlayerScript.hexPos );
mainCam.SetUserControl( true );

WorldManager.GetInstance().GetComponent( WorldManager ).remainingActions -= 1;
WorldManager.GetInstance().SetState( eCurrentState.eIdle );

}

The problem is that the few lines of code after HexManager.GetInstance().MovePlayer( newPos ) are being called before MovePlayer is finished. I can't find a way to translate smoothly without a coroutine. Any help would be greatly apreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Jul 24, 2010 at 02:19 AM

HexManager.GetInstance().MovePlayer( newPos );

needs to be

yield HexManager.GetInstance().MovePlayer( newPos );
Comment
Add comment · Show 2 · Share
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 Mike 4 · Jul 24, 2010 at 02:32 AM 0
Share

I have tried that many of times. It just sits in a function somewhere. It doesn't freeze but it never moves the player.

avatar image Eric5h5 · Jul 24, 2010 at 06:08 AM 0
Share

@$$anonymous$$ike: I don't know what World$$anonymous$$anager.GetInstance() is doing. $$anonymous$$aybe try "yield StartCoroutine(Hex$$anonymous$$anager.GetInstance().$$anonymous$$ovePlayer( newPos ));" or else use a different way of accessing $$anonymous$$ovePlayer so it can run as a coroutine.

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

No one has followed this question yet.

Related Questions

Yield everytime boolean is True (C#) 1 Answer

Waiting for input using yield and coroutines 1 Answer

StartCoroutine important for using yield? 1 Answer

How to use yield within a class function 2 Answers

JS Wait for a function to complete - no WaitForSeconds 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