• 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 RKM_91 · Apr 11, 2015 at 11:07 PM · scoreunity 4.6

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


Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DoTA_KAMIKADzE · Apr 12, 2015 at 12:00 AM

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.

Comment
Add comment · 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
0

Answer by hbalint1 · Apr 11, 2015 at 11:24 PM

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

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 RKM_91 · Apr 11, 2015 at 11:46 PM 0
Share

When I change count = count + 2 to count = count + 1 the score doesn't update when I get question 1 correct. It doesn't update the score to 2.

avatar image
0

Answer by i7x · Apr 12, 2015 at 09:17 AM

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

Comment
Add comment · 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
0

Answer by i7x · Apr 13, 2015 at 05:26 AM

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.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Scoring System Unity 6.4 C# 2D Game 1 Answer

Why doesn't my highscore script work? 3 Answers

Accessing Script From Other Script Causes Lag? 1 Answer

Scoring System 2 Answers

Saving Score In Player Prefs? 3 Answers


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