- Home /
Trouble setting a bool value between two objects
I am trying to make a quiz style sound match game, where a sound is played, and the player must choose between two balls which play similar sounds. One is of the same pitch and one is slightly different. I am trying to set a bool between the right answer(true) and wrong answer(false) but cannot understand the answers I have read about getComponent and using it to change the bool.
Here is my code for the ball1 (the script with the bool in it, and also the correct answer:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball1Click : MonoBehaviour {
public bool isCorrect;
public AudioSource beep;
private ball2Click truthScript;
// Use this for initialization
void Start () {
isCorrect = false;
}
// Update is called once per frame
void Update () {
}
void OnMouseDown(){
beep.Play();
isCorrect = true;
}
}
Here is the code for the other ball, the wrong answer that is supposed to get the component and change it to false if clicked on:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball2Click : MonoBehaviour {
public AudioSource beep;
public ball1Click boolTruth;
void Start () {
//GameObject ball1 = GameObject.FindGameObjectWithTag (ball1);
//boolTruth = ball1.GetComponent<ball1Click> ();
}
void Update () {
}
void OnMouseDown(){
ballTruth.GetComponent (ball1Click).OnMouseDown (false);
beep.Play();
boolTruth.isCorrect = false;
}
}
so you're trying to access the bool from the other script using this code: boolTruth.isCorrect = false;
?
Answer by tmalhassan · Oct 01, 2017 at 06:55 PM
Hello @mgiga0420.
I haven't checked the logic of your code. But from what I see, you're trying to access the bool in an incorrect way. To access that bool from another script, your code should look like this:
Ball1Click:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball1Click : MonoBehaviour
{
public static Ball1Click Instance {set; get;}
public bool isCorrect;
public AudioSource beep;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start()
{
isCorrect = false;
}
void OnMouseDown()
{
if (isCorrect == false)
{
beep.Play();
isCorrect = true;
}
}
}
Ball2Click:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball2Click : MonoBehaviour {
public AudioSource beep;
void Start()
{
//GameObject ball1 = GameObject.FindGameObjectWithTag (ball1);
//boolTruth = ball1.GetComponent<ball1Click> ();
}
void OnMouseDown()
{
if (Ball1Click.Instance.isCorrect == true)
{
beep.Play();
Ball1Click.Instance.isCorrect = false;
}
}
}
Also take note that the name of the class always starts with an upper case letter.
I removed the reference of the second script in the first one because I found no use for it. you can always add it back the same way I referenced the first script.
Keep in mind that I made a lot of assumptions to what you want, but you can always reply back and explain more so people could assist you furthermore.
This code did work for me, but for reference the way I fixed my code before reading this is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball2Click : $$anonymous$$onoBehaviour {
public AudioSource beep;
public ball1Click boolTruth;
void On$$anonymous$$ouseDown(){
beep.Play();
boolTruth.isCorrect = false;
}
}
The public variable receives my ball1 script, and isCorrect's value is changed on click of the object in this script, then an answer button checks the correct value of ball1's script, and either passes or fails you.