- Home /
Weird Game Breaking Variable Bug
I'm been programming a quiz type game with the new 4.6 UI system. And i have an answer variable that ranges 1 - 4 for the toggle group. Currently if i try to build with a change to this variable for testing, it dose not update in unity, IE: if it was answer 2 that was correct it would remain correct regardless of the code alterations. I tracked it down to void start() when it has been changed by then back to the original number. Though during the running of the game the variable can be changed.
At first i thought it was a simple sync bug with Visual Studio, but even to the extent of restarting the computer its the same (i did re sync and restart unity and VS). It would only update if i changed the name and then number of the variable. Also to be more painful it only occurs with this particular variable.
I was wondering if anyone has had the problem before, and if anyone knew how to fix it.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UIManager : MonoBehaviour {
private int curr_canvas; //Main: 0, Esc: 1, Settings: 3, Help: 4, Teaching: 5, Quizzing: 6
public GameObject[] canvas;
public string[] Quiz = { "Question 1", "How is the weather today?", "Cloudy", "Sunny", "Rainy", "Snowy" };
public GameObject[] quiz_canvas; //Title, Question, Q1, Q2, Q3, Q4
public GameObject[] toggle;
public int Anwser = 1;
public bool Q_1;
public bool Q_2;
public bool Q_3;
public bool Q_4;
public bool Correct;
void Start () {
curr_canvas = 0;
SetCanvas(0);
}
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
Debug.Log("SettingCanvas");
switch (curr_canvas)
{
case 0:
Debug.Log("SetCanvas(1)");
SetCanvas(1);
break;
case 1:
Debug.Log("SetCanvas(0)");
SetCanvas(0);
break;
case 2:
Debug.Log("SetCanvas(1)");
SetCanvas(1);
break;
case 3:
Debug.Log("SetCanvas(1)");
SetCanvas(1);
break;
case 4:
Debug.Log("SetCanvas(1)");
SetCanvas(1);
break;
case 5:
Debug.Log("SetCanvas(0)");
SetCanvas(1);
break;
case 6:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 7:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 8:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 9:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 10:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 11:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 12:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
case 13:
Debug.Log("SetCanvas(1)");
SetCanvas(0);
break;
default:
Debug.Log("SetCanvas(0)");
SetCanvas(1);
break;
}
}
if (Input.GetKeyDown(KeyCode.P))
{
SetCanvas(4);
SetQuiz();
}
if (Input.GetKeyDown(KeyCode.O))
{
SetCanvas(5);
}
}
public void SetCanvas(int showCanvas)
{
curr_canvas = showCanvas;
int i = 0;
do
{
if (i == curr_canvas)
{
canvas[i].SetActive(true);
}
else
{
canvas[i].SetActive(false);
}
i++;
} while (i < canvas.Length);
}
public void SetQuiz()
{
quiz_canvas[0].GetComponent<Text>().text = Quiz[0]; //Title
quiz_canvas[1].GetComponent<Text>().text = Quiz[1]; //Text Area
quiz_canvas[2].GetComponent<Text>().text = Quiz[2]; //Q1
quiz_canvas[3].GetComponent<Text>().text = Quiz[3]; //Q2
quiz_canvas[4].GetComponent<Text>().text = Quiz[4]; //Q3
quiz_canvas[5].GetComponent<Text>().text = Quiz[5]; //Q4
}
public void CheckQuiz()
{
Debug.Log("Anwser Before : " + Anwser);
SetAnwser(Anwser);
if (toggle[0].GetComponent<Toggle>().isOn == true && Q_1)
{
Correct = true;
Debug.Log("Correct: True 1");
}
else if (toggle[1].GetComponent<Toggle>().isOn == true && Q_2)
{
Correct = true;
Debug.Log("Correct: True 2");
}
else if (toggle[2].GetComponent<Toggle>().isOn == true && Q_3)
{
Correct = true;
Debug.Log("Correct: True 3");
}
else if (toggle[3].GetComponent<Toggle>().isOn == true && Q_4)
{
Correct = true;
Debug.Log("Correct: True 4");
}
else
{
Correct = false;
Debug.Log("Correct: False");
}
alpha++;
}
public void SetAnwser(int anwser)
{
Debug.Log("Anwser Int: " + anwser);
if (anwser == 1)
{
Q_1 = true;
Q_2 = false;
Q_3 = false;
Q_4 = false;
Debug.Log("Anwser 1");
}
if (anwser == 2)
{
Q_1 = false;
Q_2 = true;
Q_3 = false;
Q_4 = false;
Debug.Log("Anwser 2");
}
if (anwser == 3)
{
Q_1 = false;
Q_2 = false;
Q_3 = true;
Q_4 = false;
Debug.Log("Anwser 3");
}
if (anwser == 4)
{
Q_1 = false;
Q_2 = false;
Q_3 = false;
Q_4 = true;
Debug.Log("Anwser 4");
}
}
}
I also apologize for some of the shoddiness with the code but it works except for the variable. The variable in question is 'anwser'.
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613163733im_/https://answers.unity.com/themes/thub/images/avi.jpg)