Property "Raycast target" does not work in animation - unity3d

TL;DR - Property "Raycast target" (from Image in Canvas) is successfully animated but has no real effect (passes events through itself when it should not).
I have Image in Canvas that I am animating. In animation I change "Raycast target" property so I can interact with buttons behind after the animation ends.
Propery changes in inspector, but has no effect - I can press buttons behind when animation hasn't finished yet and "Raycast target" is "True".
When I do not use animation and change propery directly through script - everything works as expected.
What am I missing?
My Scene:
Canvas
|-Buttons
|-Image (Foreground, Animated)
My animated Image:
My Animation:
Note: "Write Defaults" in animator does not affect anything.
Unity 2020.3.8.f1 LTS

I think raycastTarget just enables Unity to consider this object for raycasting events. Turning it on or off may not make any difference to the elements behind it. I am not sure of it without testing but they have stated a single line for the raycastTarget property in the Documentation which does not help too much about this case.
I am sure that there is a blockRaycasts property on CanvasGroup component in Unity. Add this component to the object and it should block the elements behind it if it has a graphic (Image or something) attached to it.

I have not found solution, so I fixed problem through script. It checks animator state and manually changes property. In my case I have "Idle" state that is active when animation is not playing.
public class RaycastTargetFix : StateMachineBehaviour
{
public string interactableState = "Idle";
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
var image = animator.GetComponent<Image>();
if (image) image.raycastTarget = !stateInfo.IsName(interactableState);
}
}
Script needs to be added to animator:
Of course raycastTarget property should not be animated, otherwise it cannot be changed through script.

Related

How to make transparent UI visible in Unity Editor?

BackGround
In Unity 2020LTS, I want to make a UI scene.
But in Game Panel, I discovered, although a animation is set at beginning (no conditions), the game will show what I see in Editor panel for a little time at first, then play the animation.
The StateMachine is Entry -> Target(Default)
I don't want show player what I see in editor, but only the first frame in animation.
I guess this is because loading level costs some time (almost 0.5 secs).
Question
So I try another way, make initial state of all objects be same as the first frame of animation.
This way work, seems just like it freeze at first frame for 0.5secs. However, I can't edit those objects visibly (Because they all are transparent in first frame).
I have tried Gizmos, but they don't work well. Besides, Gizmos makes me have to create lots of classes in C# scripts for each object, which just is component of animation and has no script.
Could there be any better way to show transparent (UI) object in editor scene only ?
Assuming you have some Game Manager script, you can add a GameObject, assigning it the UI element, and in the Start() function of the script, make it inactive, like so:
public class GameManager : MonoBehaviour
{
public GameObject menu;
void Start()
{
menu.SetActive(false);
//other statements
}
}
I'm not quite sure what you asked for but if you want to edit the UI elements that are invisible you can simply select them by using the hierarchy and edit them. I have linked an image with an example of this. Example scene with invisible panel
Image img;
// Start is called before the first frame update
void Start()
{
img = GetComponent<Image>();
img.color = new Color32(0, 0, 0, 0);
}
The code above will set the panels transparency to zero when the scene starts, make sure to use the using unity.UI namespace in order to acess the image component.

Unity: I can not access to GameObject.SetActive() if there is an Animation that modifies it?

I am new in Unity and I don't know if I am missing something. I have found this problem repeatedly.
I have a GameObject that I deactivate/activate by code.
The same GameObject is deactivate/activate by animation(s).
Well, this doesn't work.
My commands in code are ignored. Even if the animation is not active right now.
It is easy to see when I am having this issue because while game is in Play I can not modify the active state of the GameObject from the Inspector. Only if I pause the game I can modify the active state in the Inspector but after un-pause the element go backs to the previous state.
I understand that the animation can override whatever is said in code. But I asume that this should happen only when the animation is active.
How can I play with the SetActive() of one GameObject in the animations but also in the code?
In general: You shouldn't mix changes via the animator with runtime changes via script.
It is very easy to lose the overview and you will not anymore know which changes happened because of the animator and which because of a script.
If this is really necessary you should rather use parenting and do e.g.
SomeParent
|-- SomeChild
and either control the SomeParent via the script and the SomeChild via Animator or the other way round.
As soon as there is an Animator where any animation state holds only any keyframe on a certain property, the Animator "locks" that property against any changes via script since the Animator is executed almost latest within one frame.
Try doing your change in LateUpdate or via Coroutine via yield return new WaitForEndOfFrame(); which would be executed after the Animator.
Or if possible you could disable the Animator for the time where you want to "manually" overwrite the property via script like
_animator.enabled = false;
theObject.SetActive(false);

Animation stuck at the beginning unity

Ok so I have a few animations on my character in unity, but when I hit play, in the Animator tab I see that the blue bar remains at the beginning. Although, if (while I am still in playmode) I uncheck the boxes for my boolean variables in animator, it works correctly. This is my code, please help:
{
foreach(AnimatorControllerParameter parameter in shaggyAnim.parameters)
{
shaggyAnim.SetBool (parameter.name, false);//stops the other animations
shaggyAnim.SetBool(anim, true);//plays the wanted animation
//for each animation I declared a bool variable inside unity animator
}
}```
Why don't you just set the Default State? You don't need to check all the animation parameters at the beginning. Just set the animation which you want as default state and unity will play that animation only at the beginning. Just make sure all other parameters in transition are false.

OnMouseOver works differently on same objects

I am a beginner in Unity and I just found out a behaviour I do not understand...
I have a prefab "cell" that I made from a sprite and I want it to change its color when my mouse is over it.
So I added a BoxCollider2D component to it as well as the following script:
public class Cell : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
private void OnMouseOver()
{
GetComponent<SpriteRenderer>().color = Color.magenta;
}
private void OnMouseExit()
{
GetComponent<SpriteRenderer>().color = Color.black;
}
}
Then when I drag and drop the "cell" prefab to the scene, it won't work (when my mouse is over the cell, nothing happens).
Same problem when I add another "cell" prefab to the scene.
But when I add a third "cell" prefab to the scene, the feature works on the 2 first cells but not on the third.
I probably missed something or there is a behaviour I do not know, anyway if someone knows why this is happening, please tell me.
Thanks!
I just tested your code in my game using a 2d box sprite and it works fine.
Video > https://youtu.be/6GP3-aV9g3g
You may want to try a couple of things to debug it.
First make sure that there is a BoxCollider2D and Rigidbody2D attached.
Make sure that there is nothing covering the boxes in the scene.
When I am having trouble with an aspect of the game I try to break it down into its simplest components. Try making a scene with nothing in it apart from the box and trying it, if that doesent work, try attaching the scrip to a non prefab object.
Try adding Debug.Log("Mouse Enter"); to the subs to check if the mouse is detected on enter, if it is detecting the mouse, maybe your spite renderer isn't working properly.
Try these things and let me know if they don't work, I would be happy to keep trying to figure it out.

Entry Animation does not play again when Animator is disabled then enabled

I have an animation I created called "FallingTile" that plays on my object on the Entry phase of the Animator. The problem is, because I need to change the color of the object throughout my game I need to toggle the Animator on only when it's being used, and then disable it again (this seems to be the only way to be able to change the object's color still). The first time the object's Animator is enabled the "FallingTile" effect runs smoothly and the Animator is disabled properly. However if I try to trigger the same object to animate again it will not do so. I don't believe Entry is only ran the first time the Animator is enabled because I use the same type of system for fading in/out background colors. Here's what I'm trying:
public void FallingTilesAnimation() {
Anim.enabled = true;
Invoke ("DisableAnimator", 1.1f);
}
private void DisableAnimator() {
Anim.enabled = false;
TileImage.material = null;
}
I also tried setting the Animator state back to Idle before disabling it to see if maybe that was the issue but it didn't seem to help.
edit: Probably worth noting my object starts with no material and one is added/manipulated in the FallingTile animation. It should go back to having no material after the animation ends to "reset" to the pre-animation object.
edit2: I realized it may be that Entry only runs the first time the Animator is enabled. For changing background colors I'm changing the active state of the entire object - setting the object to active likely triggers Entry again but enabling the Animator a second time may not?