2016-07-30 3 views
0
public class green : MonoBehaviour 
{ 
    private AudioSource source; 
    public AudioClip sound; 
    static int result = 0; 

    // Use this for initialization 
    void Start() 
    { 
     StartCoroutine("RoutineCheckInputAfter3Minutes"); 
     Debug.Log("a"); 
    } 

    IEnumerator RoutineCheckInputAfter3Minutes() 
    { 
     System.Random ran = new System.Random(); 
     int timeToWait = ran.Next(1, 50) * 1000; 
     Thread.Sleep(timeToWait); 

     source = this.gameObject.AddComponent<AudioSource>(); 
     source.clip = sound; 
     source.loop = true; 
     source.Play(); 

     System.Random r = new System.Random(); 
     result = r.Next(1, 4); 
     Debug.Log("d"); 

     yield return new WaitForSeconds(3f * 60f); 

     gm.life -= 1; 
     Debug.Log(gm.life + "값"); 
     source.Stop(); 
     Debug.Log("z"); 
     if (gm.life >= 0) 
     { 
      StartCoroutine("RoutineCheckInputAfter3Minutes"); 
     } 
    } 

    // Update is called once per frame 
    public void Update() 
    { 
     if (result == 1 && gm.checkeat == true) 
     { 
      Debug.Log("e"); 

      gm.life += 1; 
      Debug.Log("j"); 
      Debug.Log(gm.life + "값"); 
      source.Stop(); 
      gm.checkeat = false; 
      StopCoroutine("RoutineCheckInputAfter3Minutes"); 
      StartCoroutine("RoutineCheckInputAfter3Minutes"); 
     } 
     if (result == 2 && gm.checkshit == true) 
     { 
      Debug.Log("f"); 

      gm.life += 1; 
      Debug.Log("o"); 
      Debug.Log(gm.life + "값"); 
      source.Stop(); 
      gm.checkshit = false; 
      StopCoroutine("RoutineCheckInputAfter3Minutes"); 
      StartCoroutine("RoutineCheckInputAfter3Minutes"); 

     } 
     else if (result == 3 && gm.checksleep == true) 
     { 
      Debug.Log("g"); 
      gm.life += 1; 
      Debug.Log(gm.life); 
      Debug.Log(gm.life + "값"); 
      source.Stop(); 
      gm.checksleep = false; 
      StopCoroutine("RoutineCheckInputAfter3Minutes"); 
      StartCoroutine("RoutineCheckInputAfter3Minutes"); 
     } 
    } 
} 

public class gm geklickt wird: MonoBehaviour {Er hält für eine Weile, wenn die Taste

static public int life = 0; 
static public bool checkeat = false; 
static public bool checkshit = false; 
static public bool checksleep = false; 

// Use this for initialization 
void Start() 
{ 

} 

// Update is called once per frame 
void Update() 
{ 

} 

public void eating(string eat) 
{ 

    Debug.Log(life + "값"); 
    checkeat = true; 
} 

public void shitting(string shit) 
{ 

    Debug.Log(life + "값"); 
    checkshit = true; 
} 

public void sleeping(string sleep) 
{ 

    Debug.Log(life + "값"); 
    checksleep = true; 

} 

}

, wenn ich auf eine Schaltfläche klicken, Programm für eine Weile anhält und dann funktioniert .. Ich denke es ist wegen Thread oder etwas ... Bitte teilen Sie Ihre Meinung .. .Wenn ich auf eine Schaltfläche klicken, stoppt Programm für eine Weile und dann funktioniert ... ich denke, es ist wegen Thread oder etwas .. . Bitte teilen Sie Ihre Meinung auf ..

+0

Ich glaube, Sie wirklich eine Notwendigkeit posten [mcve]. Im Moment haben Sie Code fehlt, so dass es schwer für uns ist zu diagnostizieren. – Enigmativity

+0

Sie sagen, das Programm stoppt "für eine Weile". Wie lange ist genau "eine Weile"? – ChrisF

+2

_Es reibt die Lotion auf seiner Haut oder sonst bekommt es den Schlauch wieder _ – MickyD

Antwort

1

Stopp mit:

Thread.Sleep(timeToWait); 

Diese Stände den gesamten Thread, in diesem Fall die Einheit vollständig aus ausgeführt wird.

Da Ihre Routinen sowieso arbeiten, verwenden Sie stattdessen:

yield return new WaitForSeconds(timeToWait); 

Und diese Zeile ändern:

int timeToWait = ran.Next(1, 50) * 1000; 

Um dies:

int timeToWait = ran.Next(1, 50); 
+0

danke! Mit deiner Hilfe behebe ich Probleme ^^ –

Verwandte Themen