Search Unity

Why won't my timer work? I've tried everything.

Discussion in 'Getting Started' started by count92, Nov 6, 2016.

  1. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    I'm just trying to have the text wait a second before the enemy does it's action because right now it doesn't show the text Player attacked because the Enemy attacked text happen so quickly.
     

    Attached Files:

  2. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    I do not understand your code, a bit messy, please try not to code that way. and do not call Update() yourself. you have code that call TimeInvoke in Update and in have code to call Update in TimeInvoke, thats infinite loop. VERY DANGEROUS.

    I am suggesting you to learn coroutine.
    Code (CSharp):
    1.  
    2.  
    3.     StartCoroutine(fct_EnemyTurn());
    4.  
    5.     IEnumerator fct_EnemyTurn() {
    6.         //show the text you want
    7.         yield return new WaitForSeconds(1);
    8.         strTurn = "Enemy";
    9.     }
    10.  
    or maybe u can try to explain a bit how your game supposed to work.
     
    Last edited: Nov 6, 2016
  3. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    I've tried that too. I was messing with Coroutine yesterday with no avail. Sorry about the messyness. my first program ever written.
     

    Attached Files:

  4. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    updated script ^
     
  5. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    How I'd like it to work would be

    Code (CSharp):
    1. void Update(){
    2. if ( playerTurn == false) // enemy's turn
    3. {
    4. // move action down to Vector3(4.5, 6.1, 0)
    5. StartCoroutine(Pause());
    6. // pause for number of seconds before the rest is exicuted so it flows a little better.
    7. number = Random.Range(1,100);
    8. if (number  >= 100) //so the enemy always attacks
    9. {
    10. player.HP = player.HP- (enemy.str - player.def); // take damage
    11. PHP.text = "" + player.HP; // reads now HP number
    12. playerTurn = true; //goes back to player's turn
    13.  
    14. IEnumerator Pause(){
    15. yield return new WaitForSeconds(3);
    16. }
    17.  
    18.  
     
  6. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    I think you have misunderstood the usage of the coroutine. Its not like a function. If you start a coroutine, it will perform the coroutine until a yield, then it will return back to your main code, but your main code will transfer the execution back to the coroutine later until the coroutine finish.


    Example:
    Code (CSharp):
    1. Awake (){
    2.   StartCoroutine(fct_StartRoundTimer());
    3. }
    4.  
    5. Update() {
    6.   //other code
    7. }
    8.  
    9. IEnumerator fct_StartRoundTimer(){
    10.   int intDuration = 60;
    11.   while (intDuration > 0) {
    12.     yield return new WaitForSeconds(1f);
    13.     Debug.Log(intDuration);
    14.     intDuration -= 1;
    15.   }
    16.   fct_RoundFinish();
    17. }

    Coroutine is like threading, but the different is it does not start another thread, but u have to tell the coroutine when to return back to the main code, code execution will pass back into the coroutine every frame, but the waitforsecond will instantly give the control back to the main code until 1 second has expired.

    I think you should explain what your game do.
     
  7. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    The game works like the old earthbound game. action text in the corner will say what just happened i.e. "player attacked, Enemy attacked ect..") the HP text lowers as the Enemy generates a random number that allows it to attack (for now that's it got to try to do more advance stuff later). Then enemy object disappears once it's HP get's <= 0. the action text in the corner starts off by reading Fight, and when you press A (attack) then the enemy's health goes down, and the action bar reads "player attacked" then the Random number between 1 and 100 happens and the enemy attack. The action bar then reads "enemy attacked" but the action happens so quickly you don't get a chance to ever read that you( the player) have attacked.
    - I hope this helps I can add my project if needed.
     
  8. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    The following is the full functioning code, copy paste this code into another c# script, dont dump all your codes, just temporary disable them, then drag the script into 1 game object. Its not perfectly what you want, but it should help you understand the logic.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestCo : MonoBehaviour {
    5.  
    6.     Coroutine coPlayer;
    7.     Coroutine coEnemy;
    8.  
    9.     int intEnemyHP = 10;
    10.     int intPlayerHP = 5;
    11.     string strPlayerAction = "";
    12.  
    13.     void Start () {
    14.         coPlayer = StartCoroutine(fct_PlayerTurn());
    15.     }
    16.  
    17.     IEnumerator fct_PlayerTurn() {
    18.         Debug.Log("Player Turn");
    19.         strPlayerAction = "";
    20.         while (strPlayerAction == "") {
    21.             yield return null;
    22.         }
    23.         yield return new WaitForSeconds(1);
    24.         if (fct_CheckVictory() == false ) coEnemy = StartCoroutine(fct_EnemyTurn());
    25.     }
    26.  
    27.     IEnumerator fct_EnemyTurn() {
    28.         Debug.Log("Enemy Turn");
    29.         intPlayerHP -= 2;
    30.         yield return new WaitForSeconds(1);
    31.         if (fct_CheckVictory() == false) coPlayer = StartCoroutine(fct_PlayerTurn());
    32.     }
    33.  
    34.     IEnumerator fct_PlayerLose() {
    35.         Debug.Log("You LOSE.");
    36.         yield return new WaitForSeconds(1);
    37.         Debug.Log("wahhahahaha.");
    38.         yield return new WaitForSeconds(1);
    39.         Debug.Log("GAME OVER");
    40.         yield return new WaitForSeconds(1);
    41.         //go back to menu
    42.     }
    43.  
    44.     IEnumerator fct_PlayerWon() {
    45.         Debug.Log("You Won.");
    46.         //play music
    47.         yield return new WaitForSeconds(1);
    48.         //load next scene
    49.     }
    50.  
    51.     bool fct_CheckVictory() {
    52.         if (intPlayerHP <= 0) {
    53.             fct_StopGame();
    54.             StartCoroutine(fct_PlayerLose());
    55.             return true;
    56.         } else if (intEnemyHP <= 0) {
    57.             fct_StopGame();
    58.             StartCoroutine(fct_PlayerWon());
    59.             return true;
    60.         }
    61.         return false;
    62.     }
    63.  
    64.  
    65.  
    66.     void fct_StopGame() {
    67.         StopCoroutine(coPlayer);
    68.         StopCoroutine(coEnemy);
    69.     }
    70.  
    71.     void Update() {
    72.         //display their hp
    73.     }
    74.  
    75.     void OnGUI () {
    76.         GUI.Label(new Rect(0, 0, 100, 100), "Player HP :" + intPlayerHP.ToString());
    77.         GUI.Label(new Rect(0, 50, 100, 100), "Enemy HP :" + intEnemyHP.ToString());
    78.  
    79.         if (strPlayerAction == "") { //required action
    80.             if (GUI.Button(new Rect(0, 100, 100, 100), "Heal")) {
    81.                 strPlayerAction = "Heal";
    82.                 intPlayerHP += 5;
    83.                 if (intPlayerHP > 10) intPlayerHP = 10;
    84.             }
    85.  
    86.             if (GUI.Button(new Rect(100, 100, 100, 100), "Attack")) {
    87.                 strPlayerAction = "Attack";
    88.                 intEnemyHP -= 1;
    89.             }
    90.         }
    91.     }
    92. }
    93.  
     
  9. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    FIXED IT THANK YOU SOOOOOO MUCH!!!!!! YOU'RE HELP WAS AWESOME!!!!
     
  10. count92

    count92

    Joined:
    Nov 6, 2016
    Posts:
    7
    Yes, I didn't under stand that I was going to have to make it itself the enemy's turn (if that make's since) I was think I could call it and then it would just wait the seconds and then move one. Thank you so much for all your help
     
  11. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    223
    No problem, didn't took much time, just wanted to point out don't rely on just the update itself, code will be very messy, hard to understand, and use more cpu as lots of code there, minimize the code that need to run per frame if possible.