I have this grid of GameObjects and i want to make not only the one transparent that the mouse rests on, but also those behind it - so i am sending a RaycastAll to lower the alpha of those hit.
RaycastHit[] hitall = Physics.RaycastAll (ray, 1000, ObjectLayer);
for (int i = 0; i < hitall.Length; i++)
{
RaycastHit hit = hitall *;*
if (hit.collider.transform.childCount > 0)*
{*
Summary: set alpha from children of hit.collider.GameObject to 0.3f*
}*
}* To reset the alpha back to normal when the mouse is not over the GOs anymore, i am using OnMouseExit on the Cube class. public void OnMouseExit()
{*
if (this.transform.gameObject.layer != 8 && this.transform.childCount > 0)*
{*
Summary: Set all childrens' transparency back to 0.7f*
}*
}* This seems to almost work, but weirdly enough when i move the mouse “inward” from the outside of the grid to the inside, the alpha of the GOs is not changed back except for the last one the cursor hit. When i move the cursor “outward”, it works just like i intended. [35121-problem.jpg|35121]* Can anybody point out to me what the problem is here? Thanks a lot in advance! All the best.
When I approach situations like this, where some temporary activity should change the state of an object, I generally try to avoid using any OnExit type of event. This is largely because certain conditions will bypass the OnExit family of calls, such as quitting or pausing and resuming the game under altered input conditions. Or, you run into weirdness like you’re currently experiencing. They just don’t seem ironclad, and I like ironclad, even at the expense of extra work and CPU workload.
If the situation permits - and it sounds like yours does - I’d let my RaycastAll method trigger a method on your objects which places them in a FadeOut state. Somewhere in LateUpdate, if FadeOut wasn’t triggered this frame, switch to FadeIn. It feels a little sophomoric to do this, since you’re creating extra work for the CPU, but if your events don’t work under the most basic conditions and you can’t determine why, this presents a workable alternative.
Alternative guess here… You’re using RaycastAll to make things transparent, which goes through colliders.
Conversely, you’re using OnMouseExit to turn things opaque, which might not go through colliders.
Due to the angle of the camera, when you move the mouse to the lower left, you reach a point where tiles overlap. OnMouseExit stops at the new blocking tile and turns the tile underneath opaque, but RaycastAll goes through and turns it transparent again.
When you move the mouse towards the upper right, OnMouseExit turns the tile opaque and RaycastAll also misses at the same time, resulting in the correct behaviour.
I’m not certain that’s how OnMouseExit works, but it seems the likeliest explanation.
AlwaysSunny has a good solution. An alternative is to save a list of the transparent tiles and turn opaque the ones that no longer appear in your next RaycastAll. (I would use the LINQ operator “Except” for this.)