Score updates incorrectly?

Hello Unity Answers,

I am making a quiz game and at the moment when the player gets the question wrong, the score remains at 0 so I progress to the next cube and answer the question correctly and the score updates to 2 instead of 1?

Is there anyway I can I fix this?

All ideas, help, suggestions welcome.

alt text

alt text

Code for 1st cube

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Question1 : MonoBehaviour {
	
	private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
	public bool question1;
	private int count;
	public Text countText;

	private bool showTimer = true;
	private float Timer = 10f;

	
	void start()
	{
		count = 0;
		SetCountText ();
	}


	void FixedUpdate(){
		if (showTimer == true) {
			Timer -= Time.deltaTime;
		}

		if (Timer <= 0f) {
			showTimer = false;
			Timer = 10f;
			Destroy (this.gameObject);
			Application.LoadLevel (Application.loadedLevel);

		}
	}

	
	void OnGUI(){
		{
			windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen 
		}
	}


	void WindowFunction (int windowID) 
	{
		// Draw any Controls inside the window here

		GUI.Label (new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
		
		if (GUI.Button (new Rect (20, 100, 100, 30), "1976")) // Correct Answer
		{
			Destroy (this.gameObject);
			count = count + 1;
			SetCountText ();
		} 

		if (GUI.Button (new Rect (280, 100, 100, 30), "1986")) //Wrong answer  
		{
			Destroy (this.gameObject);
			//Application.LoadLevel(Application.loadedLevel);
		}

		if (GUI.Button (new Rect (20, 150, 100, 30), "1996")) // wrong answer
		{
			Destroy (this.gameObject);
			//Application.LoadLevel(Application.loadedLevel);
		}

		if (GUI.Button (new Rect (280, 150, 100, 30), "1966")) // wrong answer
		{
			Destroy (this.gameObject);
			//Application.LoadLevel(Application.loadedLevel);
		}

		if (showTimer == true) 
		{
			GUI.Label (new Rect (300, 25, 200, 50), "Timer: " + Timer.ToString ("F0"));
		}



	}

	void SetCountText()
		{
			countText.text = "Score: " + count.ToString ();
		}


}

Code for 2nd cube

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Question2 : MonoBehaviour {
	
	private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
	public bool question2;
	private int count;
	public Text countText;

	private bool showTimer = true;
	private float Timer = 10f;

	void start()
	{
		count = 0;
		SetCountText ();
	}

	void FixedUpdate(){
		if (showTimer == true) {
			Timer -= Time.deltaTime;
		}
		
		if (Timer <= 0f) {
			showTimer = false;
			Timer = 10f;
			Destroy (this.gameObject);
			Application.LoadLevel (Application.loadedLevel);
			
		}
	}
	
	void OnGUI()
	{
		windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
	}
	
	
	void WindowFunction (int windowID) 
	{
		// Draw any Controls inside the window here
		GUI.Label(new Rect (30, 25, 300, 50), " What is the Ebola virus named after?"); // Question
		
		if (GUI.Button (new Rect (20, 100, 100, 30), "A river"))//Correct answer
		
		{
			Destroy (this.gameObject);
			count = count + 2;
			SetCountText ();
		} 


		if (GUI.Button (new Rect (280, 100, 100, 30), "A farm")) // Wrong answer
		{
			Destroy (this.gameObject);
			//Application.LoadLevel(Application.loadedLevel);
		}
		 
		if (GUI.Button (new Rect (20, 150, 100, 30), "A tree")) // Wrong answer
		{
			Destroy (this.gameObject);
			//Application.LoadLevel(Application.loadedLevel);
		}

		if (GUI.Button (new Rect (280, 150, 100, 30), "A city")) //Wrong Answer
		{
			Destroy (this.gameObject);
			//Application.LoadLevel(Application.loadedLevel);
		}

		if (showTimer == true) 
		{
			GUI.Label (new Rect (300, 25, 200, 50), "Timer: " + Timer.ToString ("F0"));
		}
	}

	void SetCountText()
	{
		countText.text = "Score: " + count.ToString ();
	}

}

The problem is that you got 2 different counters on each question script.

Now you have few options how to do what you expect:

  1. Probably the best approach. Create a custom class that will hold your counter as a static value:

    internal class ValuesHolder
    {
    internal static int answersCount = 0;
    }

  2. Pass the value from each question to one another.

  3. Get value in every other question from question #1 and store back to question #1.

  4. etc.

P.S. And just in case - how to work with example #1:

//call this line to increment:
ValuesHolder.answersCount++;
//get value for your count text like that:
countText.text = "Score: " + ValuesHolder.answersCount.ToString();

P.P.S. Also note that static value will persist on scene changes, so if you’ll need to reset it instead then just change it to 0.

P.P.P.S. You can also make that #1 class inherit MonoBehavior and all your question classes can inherit it so that you’ll be able to call it just like:

answersCount++;

e.g.:

ValuesHolder : MonoBehaviour
////////////
Question2 : ValuesHolder

You’ll also be able to use protected variables then as well.

count = count + 2 at the second cube? you should add 1,not?

on question 2 you have count = count + 2. so when you get question 2 right the score is 2

actually you have a count variable in each script so each question starts with a count of 0. You should have a script on the score text object and have each question script access that variable and modify it. other wise if you get question 1 correct and set the text to count it will be 1 then question two comes which has a new variable count initialized at 0 when you get question 2 right count goes to 1 and you set score to count which is 1.