- Home /
trouble with referencing a componenet (bool)
I was trying to be able to reference a bool so when i click on an object, the script's bool that's attached to the object turns from false to true, and another script recognizes it and prints out a line of code (here are my two scripts) using UnityEngine; using System.Collections;
using UnityEngine;
using System.Collections;
public class BlackClick : MonoBehaviour
{
public bool buttonSwitcher1 = false;
// Use this for initialization
void awake ()
{
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnMouseUp ()
{
if (tag == "BlackButton 1")
{
Debug.Log ("Button1's bool is set to true");
buttonSwitcher1 = true;
}
}
}
and
using UnityEngine;
using System.Collections;
public class ButtonManager : MonoBehaviour
{
public GameObject blackButtonReturnBool1;
private BlackClick blackClick;
public Transform whiteButton1;
public Transform blackButton1;
public Transform buttonSlot1;
public bool buttonSwitch1 = false;
void Awake ()
{
blackClick = blackButtonReturnBool1.GetComponent<BlackClick> ();
}
// Use this for initialization
void Start ()
{
GameObject whiteSpawn1 = Instantiate(whiteButton1, buttonSlot1.position, buttonSlot1.rotation) as GameObject; // the script BlackClick is attached to this whitebutton1 object that will spawn
}
// Update is called once per frame
void Update ()
{
if (blackClick.buttonSwitcher1 == true)
{
Debug.Log ("Button 1 was switched to black");
}
}
}
my ButtonManager is set to an empty object and my BlackClick is set to the "whiteButton1" that spawns. When "whiteButton1" is clicked, BlackClick (the script attached to it) will print it's debug log "Button1's bool is set to true" but ButtonManager's debug log "button 1 was switched to black" won't print out. Does anyone have any idea as to why the if statement wasn't called?
Answer by JigneshKoradiya · Mar 27, 2015 at 09:42 PM
using UnityEngine; using System.Collections;
public class BlackClick : MonoBehaviour {
void OnMouseUp ()
{
if (tag == "BlackButton 1" && buttonSwitcher1==false)
{
Debug.Log ("Button1's bool is set to true");
buttonSwitcher1 = true;
}
}
}
and
using UnityEngine; using System.Collections;
public class ButtonManager : MonoBehaviour {
public GameObject blackButtonReturnBool1;
private BlackClick blackClick;
void Update () {
if (blackClick.buttonSwitcher1 == true)
{
Debug.Log ("Button 1 was switched to black");
blackClick.buttonSwitcher1=false;
}
}
}
Answer by hostShadows · Mar 29, 2015 at 03:04 PM
the if statement wont run, "Button 1 was switched to black" wont print, on my other object, blackClick's bool will be set to true, but it wont run my if statement on my game manager object..
i was having a problem doing this because only BlackClick's debug log was printing, but i was doing this with multiple bools. So i deciding to flip it around and make black click reference button manager, thank you for the help!
Your answer
Follow this Question
Related Questions
Referencing all objects in scene 2 Answers
Having trouble referencing a script 2 Answers
How can i reference the scripts of multiple Instantiated GameObjects? 3 Answers
This makes no sense help, pls help 1 Answer
Using a Scriptable Object as a Config 0 Answers