Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by juhainamtc · Mar 17, 2019 at 04:38 AM · increment

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Mar 17, 2019 at 06:58 AM 0
Share

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

avatar image juhainamtc toddisarockstar · Mar 17, 2019 at 07:30 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");

 }


}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image juhainamtc · Mar 17, 2019 at 09:07 AM 0
Share

@toddisarockstar looking forward to a response as im working on a project

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges