- Home /
SendMessage and boolean... my GO doesn't keep the value.
Hi,
So this is probably a really dumb question since it's my first project in Unity, but here goes anyways.
To clarify things, the plane is a "card" that when the user hover over it with the mouse, a selection gizmo should appear (visual feedback). Now, when the mouse is over the plane, it sends "true" via the gizmo's Switch function. So far, this works. The problem is that at Update(), the value is set back to false every time.
There is nothing else in my project besides the plane (card) and the gizmo.
I have this plane with this script:
public GameObject gizmo;
void OnMouseOver()
{
gizmo.GetComponent<GizmoBehavior>().Switch(true);
}
void OnMouseExit()
{
gizmo.GetComponent<GizmoBehavior>().Switch(false);
}
and then this simple Script attached to my "gizmo":
private bool bIsVisible;
// Update is called once per frame
void Update () {
Debug.Log(bIsVisible);
}
public void Switch(bool state)
{
// true = on (visible)
// false = off (not visible)
bIsVisible = state;
Debug.Log("Switch : " + bIsVisible);
}
Now, this is the console output:
False
UnityEngine.Debug:Log(Object)
Switch : true
UnityEngine.Debug:Log(Object)
... Meaning the value is correctly passed to my gizmo but somehow at Update() the value is reset back to false. I don't understand why.
Answer by Bicko · Jan 10, 2012 at 02:45 PM
I suspect that what's happening is that your gizmo is popping up on top of your card, thus your mouse is actually over the gizmo and not the card. Of course this means that the gizmo is then turned off because you're not over the card, and then you're over the card again.. ad infinitum.
No, it's not that. The feature works: when i hover the card, my Debug shows that it send "true" as a value to my gizmo, but at Update(), my gizmo still has "false" as a value for bIsVisible.
Besides, all the code you see, is all the code i have in my project right now, there's nothing else.
Really weird, it's working perfectly for me when I put those 2 scripts together and put them on 2 separate objects.. you're obviously using the right object for gizmo since you're not getting a NullReferenceException.
In my Debug I'm getting this:
False
UnityEngine.Debug:Log(Object)
False
UnityEngine.Debug:Log(Object)
Switch : True
UnityEngine.Debug:Log(Object)
True
UnityEngine.Debug:Log(Object)
Switch : True
UnityEngine.Debug:Log(Object)
True
UnityEngine.Debug:Log(Object)
Well, i seem to have found a solution, but i have no idea why.
this works:
//gizmo.GetComponent<GizmoBehavior>().Switch(true);
GameObject test = GameObject.Find("SelectGizmo");
test.GetComponent<GizmoBehavior>().Switch(true);
Any idea why?
Not sure, it should be working! (It certainly is here). Here's the whole Script I made off your one, maybe something weird is going on?
using UnityEngine;
using System.Collections;
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour
{
public GameObject gizmo;
void On$$anonymous$$ouseOver()
{
gizmo.GetComponent<GizmoBehavior>().Switch(true);
}
void On$$anonymous$$ouseExit()
{
gizmo.GetComponent<GizmoBehavior>().Switch(false);
}
}
Weird. I have the exact same script. Thanks for the help, anyway! ;)
Answer by ahottev · Jan 10, 2012 at 03:53 PM
Ok, i found the answer, but if anyone could explain why it works, i'd feel a little more intelligent.
Here's the solution:
//gizmo.GetComponent<GizmoBehavior>().Switch(true);
GameObject test = GameObject.Find("SelectGizmo");
test.GetComponent<GizmoBehavior>().Switch(true);
Why is this working actually?