- Home /
Why my bool isnt working
So my code is taken from here https://www.youtube.com/watch?v=G9QDFB2RQGA&t=919s, but this code is Onbutton system and my game is using OntriggerEnted2D, because i want my player collision answers. I can get only wrong answers or correct, but right now this code is only getting wrong answers. Inspector is showing isCorrect is true, but Debug.Log is writing only wrong answer... I dont know what to do. And there is more problems coming, because sometimes after first question there may not be right answers at all. I dont know is this happening because i Destroy(gameObject); and i have put right picture too what question is... I am bit late for this project and i want end this nightmare, because i dont have a clue what to do anymore and i am very noob at c#. I would be very grateful if somebody can help. My codes are in comments.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AnswerScript : MonoBehaviour
{
public bool isCorrect = false;
public QuizManager quizManager;
public void Answer()
{
if (isCorrect)
{
quizManager.Correct();
Debug.Log("Correct Answer");
}
else
{
quizManager.Wrong();
Debug.Log("Wrong Answer");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class QuizManager : MonoBehaviour
{
public List<QuestionsAndAnswers> QnA;
public GameObject[] options;
public int currentQuestion;
public GameObject ImagePanel;
public GameObject Quizpanel;
public GameObject GoPanel;
public Text QuestionTxt;
public Text ScoreTxt;
int totalQuestions = 0;
public int score;
private void Start()
{
totalQuestions = QnA.Count;
GoPanel.SetActive(false);
GenerateQuestion();
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void GameOver()
{
ImagePanel.SetActive(false);
Quizpanel.SetActive(false);
GoPanel.SetActive(true);
ScoreTxt.text = score + "/" + totalQuestions;
}
public void Correct()
{
score += 1;
QnA.RemoveAt(currentQuestion);
StartCoroutine(WaitForNext());
}
public void Wrong()
{
QnA.RemoveAt(currentQuestion);
StartCoroutine(WaitForNext());
}
IEnumerator WaitForNext()
{
yield return new WaitForSeconds(1);
GenerateQuestion();
}
void SetAnswers()
{
for (int i = 0; i < options.Length; i++)
{
options[i].GetComponent<AnswerScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentQuestion].Answers[i];
if (QnA[currentQuestion].CorrectAnswer == i + 1)
{
options[i].GetComponent<AnswerScript>().isCorrect = true;
Debug.Log("Ball" + i + "is right!");
}
}
}
void GenerateQuestion()
{
if (QnA.Count > 0)
{
currentQuestion = Random.Range(0, QnA.Count);
QuestionTxt.text = QnA[currentQuestion].Question;
ImagePanel = QnA[currentQuestion].Image;
SetAnswers();
}
else
{
GameOver();
Debug.Log("Out of Questions");
}
}
}
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class DestroyBall : MonoBehaviour
{
public AnswerScript answerScript;
// Start is called before the first frame update
private void OnTriggerEnter2D(Collider2D collision)
{
answerScript.Answer();
Destroy(gameObject);
Debug.Log("Osuma!");
}
}
using UnityEngine;
[System.Serializable]
public class QuestionsAndAnswers
{
public string Question;
public string[] Answers;
public int CorrectAnswer;
public GameObject Image;
}
Answer by Riiich · Sep 01, 2021 at 07:38 PM
It seems you're only using a single AnswerScript
but it's hard to tell because I'm not sure how you're using these scripts. What does your scene look like? Could you show a few screenshots of the inspector on several of the questions?
Most people upload to Imgur - it's better to keep everything here in case other people want to help - can you record a video of what happens?
Ok. ty for that info. https://imgur.com/a/1bZdcv6
I'm honestly not sure, my most used tip when debugging is to use Debug.Log("NameOfClass.cs:NameOfMethod() - extra info");
every single time a method is called and every step in the process to find out where the issue lies
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class DestroyBall : MonoBehaviour
{
public AnswerScript ball;
// Start is called before the first frame update
private void OnTriggerEnter2D(Collider2D collision)
{
ball = gameObject.GetComponent<AnswerScript>();
ball.Answer();
Destroy(gameObject);
Debug.Log("Osuma!");
}
}
Answer by bubzy · Sep 01, 2021 at 09:45 PM
you've made it public, it will permanently be set to whatever state it is in in the inspector
Your answer
![](https://koobas.hobune.stream/wayback/20220613042438im_/https://answers.unity.com/themes/thub/images/avi.jpg)