How to reset animation

I have a animation that plays, and I run the function DisablePlayer when the animation ends, but when I renable the player its still stuck on the last frame of the animation, i tried stuuf but its not working, heres my code

    public void DisablePlayer()
    {
        gameObject.SetActive(false);
    }

    void DestroyAllObjects()
    {
        GameObject[] objects = GameObject.FindGameObjectsWithTag("Asteroid");
        foreach (GameObject obj in objects)
        {
            Destroy(obj);
        }
    }

    public void EnablePlayer()
    {
        gameObject.SetActive(true);
    }
}

The answer will require restarting the anim yourself, or perhaps setting it to loop, or using an Animator and states.

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:

1 Like

If I’m understanding this question correctly, you call EnablePlayer() manually and want it to change the state of the AnimatorController there, correct? If so, you can call gameObject.GetComponent<Animator>().Play("InitialStateNameWhateverYouCalledItInYourController");

It isnt working, I think because I have nothing set for the default animation so it doesnt know what to play, because its still stuck on the last frame.

Sounds like you wrote a bug… and that means… time to start debugging!

First, work through the animation stuff I already posted above.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

1 Like

thank you i will try and get back to you

Screenshot your animator controller and I can tell you, but it’s just whatever is the default state. You can’t have an animator controller without one. Or is this using the legacy animation component?