0

Currently i'm developing a quiz kind of game for my gamification project. where i freeze the actual game when the player reaches some score and make question appear. If the answer is correct it will increase the speed of the player.And it will happen in a loop.

Here i'm using loadscene to load the index scene to avoid repeating of questions. The problem here is when the scene is loads it reloads the whole game instead of reloading the quiz part. Is there any way to do it ?

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());
    }
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
METEOARA
  • 3
  • 3

1 Answers1

1

Loading the scene again basically restarts it, what you want to do is change the TransitionToNextFact method in the following manner

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
}

i would also combine the two methods UserSelectTrue and UserSelectFalse into one

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

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

    }


    StartCoroutine(TransitionToNextFact());
}
Ido Ben Shalom
  • 562
  • 4
  • 12
  • Thank you so much ! helped me a lot. The only problem i faced is that the method UserSelected(), for some reason doesn't trigger the animation and start the couroutine. But if i use the previous methods UserSelectTrue() and UserSelectFalse() it works fine. I think the problem is in IsTrue.ToString(). – METEOARA Feb 28 '17 at 09:44
  • no problem at all! I edited the answer, that should definitely fix the trigger problem. if you found this answer helpful please consider marking it as such :) – Ido Ben Shalom Feb 28 '17 at 09:53
  • Sorry man ! Everything works fine. It was my mistake i forgot to add the method in On Click(). – METEOARA Feb 28 '17 at 09:57
  • Hey ! As you understand my code. Can you take look at this .. https://stackoverflow.com/questions/42594257/how-assign-values-from-a-csv-file-to-a-array-or-list – METEOARA Mar 04 '17 at 10:28