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;
    }

}

}

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.

@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
     }
 }
}