Search Unity

[Code Attached] C# not waiting after player attack before enemy attack

Discussion in 'Scripting' started by Josenifftodd, Dec 23, 2014.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    So I'm new to C# as I've only used JS before now I'm currently converting my JS based battle system to C# using the new uGUI as I was told it will make performance better.
    I've come up with this so far does what I want but doesn't wait 2 seconds at all its unless it is and 2 seconds is really fast. Can anyone help with the problem? I know they is unwanted stuff there I'm still getting use to using c#

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class displayTextScript : MonoBehaviour {
    6.     public GameObject textGuiObj;
    7.     public Text gameText;
    8.     public int playerHealth;
    9.     public Button gameButton;
    10.     public bool turnForAttack = false;
    11.     public int turn = 1;
    12.     public int phase = 0;
    13.     GameObject Button;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         gameText.text = ("Attack");
    19.  
    20.         //gameButton.interactable = false;
    21.         Test();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         Test();
    27.     }
    28.  
    29.     public void Clicked() {
    30.         turn = 0;
    31.         phase = 1;
    32.         Example () ;
    33.  
    34.     }
    35.  
    36.     public void Test ()
    37.     {
    38.        
    39.         if(turn > phase)
    40.         {
    41.            
    42.             print("player attack");
    43.             gameButton.interactable = true;
    44.  
    45.         }
    46.        
    47.         else if(phase > turn)
    48.         {
    49.             gameButton.interactable = false;
    50.             print("This is the enemy turn");
    51.             Example () ;
    52.             phase = 0;
    53.             turn = 2;
    54.  
    55.  
    56.  
    57.            
    58.         }
    59.        
    60.         else
    61.         {
    62.             gameButton.interactable = false;
    63.             print("something");
    64.         }
    65.     }
    66.     IEnumerator Example() {
    67.         print(Time.time);
    68.         yield return new WaitForSeconds(5);
    69.         print(Time.time);
    70.     }
    71. }  
    72.  
     
  2. FreeTimeDev

    FreeTimeDev

    Joined:
    Jul 7, 2012
    Posts:
    64
    When will turn ever be greater than phase?
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    When you call a coroutine in c# you have to use start:
    StartCoroutine(Example());
     
  4. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    on clicked makes turn greater than phase :/ after player go everything kinda gets reversed, least thats what I think it's doing, its not even like this on my JS version :/ having a little hard time converting it over well learning lol
    Sure I tried something with that way and got a error, c12 something ? could you do an example of how to use that with another function?
     
  5. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373