Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 12:56 PM · bool

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.

Comment
Add comment · Show 5
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 unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 12:59 PM 0
Share
 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");
         }
     }
 
 }
 
 
avatar image unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 01:01 PM 0
Share
 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");
         }
 
 
     }
 }
 
 
avatar image unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 05:39 PM 0
Share
 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!");
     }
 }
avatar image unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 05:41 PM 0
Share
 using UnityEngine;
 
 [System.Serializable]
 public class QuestionsAndAnswers
 {
     public string Question;
     public string[] Answers;
     public int CorrectAnswer;
     public GameObject Image;
 }
avatar image unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 05:47 PM 0
Share

alt text

unknown.png (58.1 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 10 · 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 unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 09:31 PM 0
Share

Yea what you want to see?

avatar image unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 09:53 PM 0
Share

pictures are too big for here

avatar image Riiich unity_w5-j_cCTLH_i3A · Sep 02, 2021 at 07:36 AM 0
Share

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?

avatar image unity_w5-j_cCTLH_i3A · Sep 02, 2021 at 08:46 AM 0
Share

Ok. ty for that info. https://imgur.com/a/1bZdcv6

avatar image Riiich unity_w5-j_cCTLH_i3A · Sep 02, 2021 at 03:57 PM 0
Share

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

avatar image unity_w5-j_cCTLH_i3A Riiich · Sep 02, 2021 at 04:14 PM 1
Share

I will try that.

avatar image unity_w5-j_cCTLH_i3A · Sep 02, 2021 at 07:47 PM 0
Share
 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!");
     }
 }
avatar image unity_w5-j_cCTLH_i3A · Sep 02, 2021 at 07:47 PM 0
Share

Now i get true and false both :)

Show more comments
avatar image
0

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

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 unity_w5-j_cCTLH_i3A · Sep 01, 2021 at 09:56 PM 0
Share

Still false.

4.png (6.6 kB)

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

126 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 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

Slerpy Spider Won't Behave (Chaining Slerps) 1 Answer

Quick question about arrays and MonoDevelop 1 Answer

NullReferenceException: When accessing bools from another game object's script. 1 Answer

Problem with creating static bool... 1 Answer

Set a Bool Through Function 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