- Home /
The question is answered, right answer was accepted
Animation Trigger
Hi! So here i have a script wich enables my screen fading animation whenever i collide with the object called: Ground [UNREACHABLE]. the animation gets disabled when i start the game, but it won't turn back on when i collide it with my player (Character).
Can someone please help me fix this! I am pretty bad at coding so i'd REALLY appriciate it if you tell me on how i can fix it. Thank you!
Fade script (c#):
public class Screen_Fade : MonoBehaviour {
public Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
anim.enabled = false;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.Equals("Ground [UNREACHABLE]"))
{
anim = GetComponent<Animator>();
anim.enabled = true;
}
}
}
Answer by Nachiten · Apr 29, 2019 at 02:15 AM
@lorenzo1812 The answer is pretty simple and should have a very easy fix. Unity cannot access nor modify hidden or disabled objects. Unless, you already have a variable assigned to them. Which is exactly what you have here.
So I dont really know the reason of why you are changing the value of the variable "anim" to exactly the same value as above. Then unity is not able to find that component as its already disabled. You just remove that and you are done. Good luck !!! I hope that works then tell me if not.
public Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
anim.enabled = false;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.Equals("Ground [UNREACHABLE]"))
{
// Removed just this line : anim = GetComponent<Animator>();
anim.enabled = true;
// This should work just fine
}
}
}
Hi again! Not sure but i changed it like you showed me, but it still doesn't work! Do you have any other solutions? I'v heard that if you disable an animation you can't turn it back on again by script, it that true?
I have been struggling to get this simple thing working for 2 days ;-; All i want is whenever my 2D character gets in contact with the collision of the [UNREACHABLE] ground, the animation starts working.
note that i am using a image on a canvas wich uses an animation that goes from zero opacity to around 65% opacity, so all i did was i used the animator, made 2 keys that go from 0 to 65%, and i only have one animation state, wich is the one from the opacity that immediatly starts when i run the game. I have the script attached to the image.
@lorenzo1812 Could you please tell me if you get any kind of error on console when playing the animation? I mean when excecuting this piece of code:
if (col.gameObject.name.Equals("Ground [UNREACHABLE]"))
{
// Removed just this line : anim = GetComponent<Animator>();
anim.enabled = true;
// This should work just fine
}
I think I may have some other solutions. Like using commands like animation.Play("NA$$anonymous$$E") and animation.Stop("NA$$anonymous$$E"). For this solution you would first need to assign your variable in this case animation and the animation component of the object adding it in the inspector beforehand and not the animator.
And make a code like so:
// Yes this is an animation
Animation animation = GetComponent<Animation>();
// To play animation [ Replace Name with your animation name]
animation.Play("NA$$anonymous$$E");
// To stop animation
animation.Stop("NA$$anonymous$$E");
This is the reference for all of the above: https://docs.unity3d.com/ScriptReference/Animation.html
Last step if none of this work would be to implement a complete Animator system using the stuff that @thenebularofficial already said above.
This is the reference for that: https://docs.unity3d.com/ScriptReference/Animator.html
Im not qualified to $$anonymous$$ch yoy about this I dont know it a lot myself. Look for tutorials there are lots of them complete and well explained on the web. Your problem should not be hard to adress. Just dont give up. Best of luck.
it worked! I did a quick search in the Unity docs you sent me, and i think i got it right! What i did was i set my animation sooo slow that it's impossible to notice (since it's a white image that slowly fades in), you could only probably see a difference in the game if you left it running for like 3 days straight, wich im pretty sure no mad man would do. The rest was pretty straight forward. I made the speed normal when i collide with the [UNREACHABLE] object, so it's looks totally normal to the people playing it, as they think it's just s simple fade transition!
Thank you for helping me, man! <3
Answer by thenebularofficial · Apr 29, 2019 at 02:08 AM
Okay, there might be multiple reasons for your problem, so here are some suggested fixes: 1.Make sure the animator's default state is the animation you want. For more information, visit this Unity Tutorial.
2.If you don't want to use your animation as default state try using Triggers, create one in the editor, make your animation to play when you activate your trigger and add this piece of code anim.SetTrigger("Your_Trigger_name");
Documentation
Also, you don't need to set anim = GetComponent<Animator>();
twice as you already set it in void Start.