Referenced Bool is not updating
I've been trying to fix this for some hours but it just won't work out.
I have two scripts. One that is attached to the player (Script1) and one that is attached to a prefab (Script2).
In Script1 I want to use a referenced bool from Script2. That bool is true when the mouse is over the prefab:
void OnMouseOver()
{
isOver = true;
}
The bool gets false when not hovering the prefab. Until here everything works. My reference in the other script looks like this:
public bool over;
public gameObject boxPrefab; // Referenced in the Inspector
void Update ()
{
over = boxPrefab.GetComponent<DestroyBox>().isOver;
}
When I now try to check with Debug.Log if over is true it won't give me any response in the console.
What is my fault? I'm sure it's something super stupid.
It is attached to the Prefab (boxPrefab).
Ok so script1 is the one starting with public bool over;
?
Are you doing Debug.Log(over)
right after over = boxPrefab.GetComponent<DestroyBox>().isOver;
?
If nothing is showing up then it's probably because script1 isn't attached to anything in the scene
Think you could post the full scripts for script1 and script2?
Posted another comment with both scripts.
Answer by fabian_rensch · Dec 17, 2015 at 10:49 PM
Both Scripts here.
Script 1 is for instantiating and counting the prefabs. Script 2 is for Destroying the clicked prefab.
Script BoxMagic aka Script1
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class BoxMagic : MonoBehaviour {
public GameObject boxPrefab;
public Vector3 pz;
public int boxAnzahl = 5;
public bool isEmpty = false;
public bool isFull = true;
public Text countText;
public bool over;
void Start ()
{
}
void Update ()
{
over = boxPrefab.GetComponent<DestroyBox>().isOver;
pz = Camera.main.ScreenToWorldPoint (Input.mousePosition);
pz.z = 0;
setBox ();
deleteBox ();
if (boxAnzahl >= 5) {
isFull = true;
} else {
isFull =false;}
if (boxAnzahl <= 0) {
isEmpty = true;
} else {
isEmpty = false;
}
if (over == true)
{
Debug.Log("over!");
}
}
void SetCountText()
{
countText.text ="Boxes: "+ boxAnzahl.ToString()+"/5";
}
void setBox()
{
if (isEmpty == false )
{
if (Input.GetMouseButtonDown (1))
{
boxAnzahl--;
SetCountText();
Instantiate(boxPrefab, pz, Quaternion.Euler(0, 180, 0));
}
}
else
{
isFull =false;
}
}
void deleteBox()
{
if (isFull == false && over == true) {
if (Input.GetMouseButtonDown (2)) {
boxAnzahl++;
SetCountText ();
}
} else {
isEmpty =false;
}
}
}
Script DestroyBox aka Script 2
using UnityEngine;
using System.Collections;
public class DestroyBox : MonoBehaviour {
public bool isOver;
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseOver()
{
isOver = true;
if(Input.GetMouseButtonDown(2))
{
Destroy(this.gameObject);
}
}
void OnMouseExit()
{
isOver = false;
}
}