2017-02-28 65 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

相关问题