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());
    }
1

1 Answer 1

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());
}
Sign up to request clarification or add additional context in comments.

4 Comments

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().
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 :)
Sorry man ! Everything works fine. It was my mistake i forgot to add the method in On Click().
Hey ! As you understand my code. Can you take look at this .. stackoverflow.com/questions/42594257/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.