- Home /
counter incrementing incorrectly
Hello. I am currently working on a quiz. I am playing a video and during the video at certain timestamps the questions are ot pop up in a quesitons canvas. There is another canvas called the Score canvas which displays th elive score as the user answers the questions. The questions are in multiple choice format. When the user clicks a button, the response value is sent to a function HandleResponse() and then the answer is cheked here. The score text is displayed as the score / the numebr of questions responded. So when the numQuestionsResponded variable is equal to 5, a button to view final score and exit video is displayed.
This worked fine until a couple of days back. I notice that the numQuestionsResponded value increments by 2 each time and the quiz ends at the 3rd question itself.
here is my code snippet
public void HandleAnswer(bool response) {
print("responded:" + response + "Correct answer" + nextQuestion.correct);
//check if the answer was correct
//hide the question canvas
questionCanvas.SetActive(false);
//increase the number of responded questions
numQuestionsResponded = numQuestionsResponded+1;
Debug.Log(numQuestionsResponded);
if (response == nextQuestion.correct)
{
totalCorrect++;
scoreText.text = "Correct!";
}
else
{
scoreText.text = "Wrong";
}
Debug.Log("Answered" + numQuestionsResponded);
scoreText.text += "\nScore:" + totalCorrect + "/" + numQuestionsResponded;
if (numQuestionsResponded == 5)
{
scoreText.text = "Final Score:" + totalCorrect + "/" + numQuestionsResponded;
PlayerPrefs.SetString("FullScore", scoreText.text);
Debug.Log(scoreText.text);
// PlayerPrefs.SetString("VideoTitle", Vidtitle.text);
btnexit.SetActive(true);
scoreText.text += "\nQuiz Over";
}
PrepareNext();
Earlier, the code to increment was numQuestionsResponded++. Just to make sure there is no problem in that, i changed it to numQuestionsResponded = numQuestionsResponded +1
What could be the reason for this?
using ++ ins$$anonymous$$d of writing out something = something + 1 does the same exact thing to an int.
your problem has to be co$$anonymous$$g from some other coding you didn't post.
either the code you posted is being called more often than you intend, or there is something else changing your variable.
look at where this code block is being called. maybe look for a mouse input that is registering every frame when it's held down. or any other code than might be run this more than you want.
then use your comilers search to ensure no other script lines are changing your varible!
sorry for the vague responce. we cant see the rest of your code
thanks for oyur response. please see my ful code in the reply below Also, i am noticing now that the totalCorrect counter is also incrementing in a werid way. Its sometimes displaying 4/6 whereas the total quesitons are on;ly 5 and the totalcorrect should be 2/3 at that timestamp of the video.
Answer by juhainamtc · Mar 17, 2019 at 08:49 AM
thanks for your reply. herwe is my complete code. I am not a pro in coding,, my code is too lengthy i feel. but here it is: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; //using UnityEngine.UI.Button; using UnityEngine.SceneManagement;
public class QuizManager : MonoBehaviour { public float gazeTime = 2f; public float myTime; private bool gazedAt; //canvas where the question appears public GameObject questionCanvas;
//Score Canvas
public GameObject scorecanvas;
//answer
public GameObject btnanswer;
public GameObject btnchoice2;
public GameObject btnchoice3;
public GameObject btnq2choice2;
public GameObject btnq2choice3;
public GameObject btnq2answer;
public GameObject btnq3choice2;
public GameObject btnq3choice3;
public GameObject btnq3answer;
public GameObject btnscore;
public GameObject btnexit;
//question title
public Text questionTitle;
//score text
public Text scoreText;
//video player
public VideoPlayer player;
//response
public bool ans;
//audio
public AudioSource audsrc;
//flag to indicate whether we are showing questions or not
bool isShowingQuestions;
//Quiz
Quiz quiz;
//time(used to show questions)
float elapsedTime = 0;
//next question
Question nextQuestion;
//ocunter for changing button text
private int i = 0;
//next question Index
int nextQuestionIndex;
//total correct answers
static public int totalCorrect = 0;
//total questions
static public int numQuestionsResponded;
public Text Vidtitle;
//this variable will be used for testing purpose for getString for video title
//public Text v2; string v;
public int counter = 0;
public bool resp;
//to exit video
public string endvideo;
public string exitvideo;
void Start () {
//v = Vidtitle.text.ToString();
Vidtitle= GameObject.Find("VideoTitle").GetComponent<Text>();
//this line is to check if the video title is being retrieved
//Debug.Log(Vidtitle.text);
PlayerPrefs.SetString("VideoTit", Vidtitle.text);
//this line is to check if the video title is stored correctly in playerprefs
Debug.Log(Vidtitle.text);
//these next two lines are to check if playerprefs gets the correct video title
//v2.text= PlayerPrefs.GetString("VideoTit");
//Debug.Log(v2.text);
//pause quiz
PauseQuiz();
//hide question canvas
questionCanvas.SetActive(false);
//hide score canvas
scorecanvas.SetActive(false);
btnscore.SetActive(false);
btnexit.SetActive(true);
//prepare quiz first(questions of the quiz)
quiz = new Quiz();
quiz.questions = new Question[5];
quiz.questions[0] = new Question();
quiz.questions[0].time = 27;
quiz.questions[0].title = "Lions like to __ running";
quiz.questions[0].correct = true;
quiz.questions[1] = new Question();
quiz.questions[1].time = 41;
quiz.questions[1].title = "The African Lion Population_____ dropped by over 50%";
quiz.questions[1].correct = true;
quiz.questions[2] = new Question();
quiz.questions[2].time = 148;
quiz.questions[2].title = "Lionesses like lions with ____, _____ manes";
quiz.questions[2].correct = true;
quiz.questions[3] = new Question();
quiz.questions[3].time = 150;
quiz.questions[3].title = "The lion moves _____ the camera";
quiz.questions[3].correct = true;
quiz.questions[4] = new Question();
quiz.questions[4].time = 156;
quiz.questions[4].title = "The lion ______ the camera";
quiz.questions[4].correct = true;
//prepare nrxt question
PrepareNext();
}
// Update is called once per frame
void Update () {
if (gazedAt&&counter==0)
{
myTime += Time.deltaTime;
if (myTime >= 3)
{
HandleAnswer(resp);
}
}
if(endvideo=="yes")
{
myTime += Time.deltaTime;
if(myTime>=3)
{
ExitVid();
}
}
if (exitvideo == "yes")
{
myTime += Time.deltaTime;
if (myTime >= 3)
{
QuitVid();
}
}
//check that we should be showing questions
if (!isShowingQuestions) return;
//increase the elapsed time by the amount that has happened since the last loop
elapsedTime += Time.deltaTime;
//check time, if a quesiton is due, show it
if(elapsedTime>nextQuestion.time)
{
//show question
//1)show question canvas
questionCanvas.SetActive(true);
scorecanvas.SetActive(true);
//2) show the question title
questionTitle.text = nextQuestion.title;
// btnanswer.GetComponentInChildren<Text>().text = "has";
//3) Pause the quiz
PauseQuiz();
}
}
void PauseQuiz()
{
//video paused
AudioListener.pause = true;
player.Pause();
//no showing question
isShowingQuestions = false;
}
void ResumeQuiz()
{
AudioListener.pause = false;
//continue playing video
player.Play();
changebtntext();
//continue playing audio
//continue measuring time elapsed
isShowingQuestions = true;
}
void PrepareNext()
{
//setting the first value
if(nextQuestion==null)
{
//set index to the start of the array
nextQuestionIndex = 0;
//get next question
nextQuestion = quiz.questions[nextQuestionIndex];
}
else
{
//increase the next question index
nextQuestionIndex++;
//check that there are more questions left
if(nextQuestionIndex < quiz.questions.Length)
{
//get next question
nextQuestion = quiz.questions[nextQuestionIndex];
}
else
{
//Quiz is over
print("Video completed!");
scoreText.text += "\nQuiz Completed!";
questionCanvas.SetActive(false);
player.Play();
AudioListener.pause = false;
return;
}
}
ResumeQuiz();
}
void changebtntext()
{
if(i==0)
{
if (btnanswer.GetComponentInChildren<Text>().text == "null")
{
btnanswer.GetComponentInChildren<Text>().text = "go";
btnchoice2.GetComponentInChildren<Text>().text = "be";
btnchoice3.GetComponentInChildren<Text>().text = "do";
btnq2answer.SetActive(false);
btnq2choice2.SetActive(false);
btnq2choice3.SetActive(false);
btnq3answer.SetActive(false);
btnq3choice2.SetActive(false);
btnq3choice3.SetActive(false);
// btnexit.SetActive(false);
i = i + 1;
Debug.Log("i = " + i);
}
}
else if (i == 1)
{
btnq2choice2.GetComponentInChildren<Text>().text = "have";
btnq2choice3.GetComponentInChildren<Text>().text = "is";
btnq2answer.GetComponentInChildren<Text>().text = "has";
btnq2answer.SetActive(true);
btnq2choice2.SetActive(true);
btnq2choice3.SetActive(true);
btnanswer.SetActive(false);
btnchoice2.SetActive(false);
btnchoice3.SetActive(false);
i = i + 1;
Debug.Log("i = " + i);
}
else if (i == 2)
{
btnq3answer.GetComponentInChildren<Text>().text = "darker,thicker";
btnq3choice2.GetComponentInChildren<Text>().text = "more dark,more thick";
btnq3choice3.GetComponentInChildren<Text>().text = "darkest,thickest";
btnq3answer.SetActive(true);
btnq3choice2.SetActive(true);
btnq3choice3.SetActive(true);
btnanswer.SetActive(false);
btnchoice2.SetActive(false);
btnchoice3.SetActive(false);
btnq2answer.SetActive(false);
btnq2choice2.SetActive(false);
btnq2choice3.SetActive(false);
i = i + 1;
Debug.Log("i = " + i);
}
else if (i == 3)
{
btnanswer.GetComponentInChildren<Text>().text = "towards";
btnchoice2.GetComponentInChildren<Text>().text = "with";
btnchoice3.GetComponentInChildren<Text>().text = "away";
btnanswer.SetActive(true);
btnchoice2.SetActive(true);
btnchoice3.SetActive(true);
btnq2answer.SetActive(false);
btnq2choice2.SetActive(false);
btnq2choice3.SetActive(false);
btnq3answer.SetActive(false);
btnq3choice2.SetActive(false);
btnq3choice3.SetActive(false);
i = i + 1;
Debug.Log("i = " + i);
}
else if (i == 4)
{
btnq2choice2.GetComponentInChildren<Text>().text = "touch";
btnq2choice3.GetComponentInChildren<Text>().text = "will touch";
btnq2answer.GetComponentInChildren<Text>().text = "touched";
btnq2answer.SetActive(true);
btnq2choice2.SetActive(true);
btnq2choice3.SetActive(true);
btnanswer.SetActive(false);
btnchoice2.SetActive(false);
btnchoice3.SetActive(false);
btnq3answer.SetActive(false);
btnq3choice2.SetActive(false);
btnq3choice3.SetActive(false);
i = i + 1;
Debug.Log("i = " + i);
}
return;
}
public void PointerEnter(bool response1)
{
resp = response1;
gazedAt = true;
counter = 0;
}
public void HandleAnswer(bool response)
{
print("responded:" + response + "Correct answer" + nextQuestion.correct);
//check if the answer was correct
//hide the question canvas
questionCanvas.SetActive(false);
//increase the number of responded questions
numQuesResponded++;
Debug.Log(numQuesResponded);
if (response == nextQuestion.correct)
{
totalCorrect++;
scoreText.text = "Correct!";
}
else
{
scoreText.text = "Wrong";
}
scoreText.text += "\nScore:" + totalCorrect + "/" + numQuesResponded;
if (numQuesResponded == 5)
{
scoreText.text = "Final Score:" + totalCorrect + "/" + numQuesResponded;
PlayerPrefs.SetString("FullScore", scoreText.text);
Debug.Log(scoreText.text);
// PlayerPrefs.SetString("VideoTitle", Vidtitle.text);
btnexit.SetActive(true);
scoreText.text += "\nQuiz Over";
}
PrepareNext();
counter += 1;
myTime = 0f;
}
public void PointerExit()
{
gazedAt = false;
myTime = 0f;
exitvideo = "no";
}
public void PointerEndvideo()
{
gazedAt = true;
endvideo = "yes";
}
public void PointerExitvideo()
{
gazedAt = true;
exitvideo = "yes";
}
public void QuitVid()
{
PlayerPrefs.DeleteAll();
SceneManager.LoadScene("Home");
}
public void ExitVid()
{
SceneManager.LoadScene("ScoreReport");
}
}
@toddisarockstar looking forward to a response as im working on a project
Your answer
Follow this Question
Related Questions
Help scoring Points In a Jousting game 1 Answer
Key Combo system. where I am going wrong with this? 2 Answers
Incrementing a value repeatedly 1 Answer
How to add score after destroying object? 1 Answer
Movement by increment jerkiness 1 Answer