2017-02-28 28 views
0

目前我正在爲我的遊戲化項目開發測驗類遊戲。當玩家達到某個分數並且出現問題時,我凍結實際遊戲。如果答案是正確的,它會提高玩家的速度,並且會在一個循環中發生。如何在不重複統一的情況下生成隨機數據C#

在這裏,我使用的是loadscene來加載索引場景,以避免重複問題。這裏的問題是當場景加載時重新加載整個遊戲而不是重新加載測驗部分。有什麼辦法可以做到嗎?

public class GameManager : MonoBehaviour 
{ 
    public Question[] facts; 
    private static List<Question> unansweredfacts; 
    private Question currentfacts; 

    [SerializeField] 
    private Text FactText; 

    [SerializeField] 
    private float TimeBetweenFacts = 3f; 

    [SerializeField] 
    private Text TrueAnswerText; 

    [SerializeField] 
    private Text FalseAnswerText; 

    [SerializeField] 
    private Animator animator; 

    [SerializeField] 
    public GameObject canvasquiz; 

    void Start() 
    { 
     if (unansweredfacts == null || unansweredfacts.Count == 0) 
     { 
      unansweredfacts = facts.ToList<Question>(); 
     } 
     SetCurrentfact(); 
     Debug.Log(currentfacts.Fact + "is" + currentfacts.IsTrue); 
    } 

    void SetCurrentfact() 
    { 
     int RandomFactIndex = Random.Range(0, unansweredfacts.Count); 
     currentfacts = unansweredfacts[RandomFactIndex]; 
     FactText.text = currentfacts.Fact; 
     if (currentfacts.IsTrue) 
     { 
      TrueAnswerText.text = "CORRECT !"; 
      FalseAnswerText.text = "WRONG !"; 
     } 
     else 
     { 
      TrueAnswerText.text = "WRONG !"; 
      FalseAnswerText.text = "CORRECT !"; 
     } 
    } 

    IEnumerator TransitionToNextFact() 
    { 
     unansweredfacts.Remove(currentfacts); 
     yield return new WaitForSeconds(TimeBetweenFacts); 
     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); 
    } 

    public void UserSelectTrue() 
    { 
     animator.SetTrigger("True"); 
     if (currentfacts.IsTrue) 
     { 
      Debug.Log("CORRECT !"); 
     } 
     else 
     { 
      Debug.Log("WRONG !"); 
     } 
     StartCoroutine(TransitionToNextFact()); 
    } 

    public void UserSelectFalse() 
    { 
     animator.SetTrigger("False"); 
     if (!currentfacts.IsTrue) 
     { 
      Debug.Log("CORRECT !"); 
     } 
     else 
     { 
      Debug.Log("WRONG !"); 
     } 
     StartCoroutine(TransitionToNextFact()); 
    } 
+0

可能的重複[Randomize a List ](http://stackoverflow.com/questions/273313/randomize-a-listt) –

回答

1

加載場景再次基本上重新啓動它,你想要做的是改變下列方式TransitionToNextFact方法

IEnumerator TransitionToNextFact() 
{ 
    unansweredfacts.Remove(currentfacts); // remove the last shown question for the list 
    canvasquiz.SetActive(false); // disables the quiz canvas until the next question 
    yield return new WaitForSeconds(TimeBetweenFacts); 

    SetCurrentfact(); // sets the next random question from the list 
    canvasquiz.SetActive(true); // show the quiz canvas along with the new question 
} 

我也將結合上述兩種方法UserSelectTrueUserSelectFalse合成爲一個

public void UserSelected(bool isTrue) 
{ 
    animator.SetTrigger(isTrue ? "True" : "False"); 
    if (currentfacts.IsTrue == isTrue) 
    { 
     Debug.Log("CORRECT !"); 

    } 
    else 
    { 
     Debug.Log("WRONG !"); 

    } 


    StartCoroutine(TransitionToNextFact()); 
} 
+0

非常感謝你!幫了我很多。我遇到的唯一問題是UserSelected()方法出於某種原因不會觸發動畫並啓動couroutine。但是,如果我使用以前的方法UserSelectTrue()和UserSelectFalse()它工作正常。我認爲這個問題在IsTrue.ToString()中。 – METEOARA

+0

完全沒問題!我編輯了答案,應該明確地解決觸發問題。如果你發現這個答案有幫助,請考慮標記爲:) –

+0

對不起!一切正常。這是我的錯誤,我忘了在On Click()中添加該方法。 – METEOARA

相關問題