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.

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.

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?