简体   繁体   中英

Unity 2D Animation Won't Play after SetActive(true)

I have a 2D single screen style game where I have a disabled gameobject. Under this are several children that that are animated. The parent object holds the animator for the children. When I enabled the parent, the animations will not play. The animator looks as thought it's trying to processes the default entry state but does not complete. I've done some research on this and it seems that disabling and then enabling something does not start the animator. I've tried writing some code for it with no luck and setting the sprite renderer false won't work either as there are simply too many sprites. Here's my last attempt: Any ideas?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animationState : MonoBehaviour
{
    public Animator anim;

    private void OnEnable()
    {
        if (anim.gameObject.activeSelf)
        {
            anim.Play("pinWheelAnimation");
        }
    }
}

Unity doesn't work like that, You can't set an animator to the parent object that is going to influence animations from children, you are going to have to set up an animator for every child. If you want to modify animators of children from a parent object you can do that with a script that searches for all children and their components and changes their parameters.

Take a look at this: Animator parameters

Try to set an IdleState as DefaultState with no exit time and no animation and define a trigger event to go over to your animation state. Then activate the trigger in

void Start()
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM