Hi, I’m working on a simple 2D platformer where the player collects various items, and I’m struggling with a simple problem that I don’t know how to solve.
The moment the player picks up an object, the animation of picking up the object should play, which is placed on the object. It’s a sort of teleportation effect.
I then have an Item_Collector script on the player that takes care of this. So as soon as the player touches the item the animation should be activated, the trigger collected is a condition of the animation. And when the animation is over, the item is removed from the scene. And the player is awarded some points, depending on what he has collected.
Animator anim;
[SerializeField] private GameObject collectibleGameObject;
private void star()
{
anim = collectibleGameObject.GetComponent<Animator>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.layer == LayerMask.NameToLayer("Collectible"))
{
if (collision.gameObject.CompareTag("SomeItem"))
{
collectionSoundEffect.Play();
anim.SetTrigger("collected");
Destroy(collision.gameObject, anim.GetCurrentAnimatorStateInfo(0).length + 0);
....
}
My code is simple, first I initialize the animator, and the gameobject.
Then in the start method, I initialize the animator of the specific gameobject and then I just play the animation if the object is collected. Then in the destroy method, I wait for the animation to finish, and then remove the object.
And here I get an error, or rather a warning: animator is not playing an AnimatorController.
Line 7 should be void Start()
, that code isn’t running.
How did it get there… Anyway, it’s not in the code I got it right plus such an entry would throw me another error
Look at this post for why you’re getting that error: https://discussions.unity.com/t/660977
It’s likely that the gameobject containing the animator isn’t active when you try referencing it in code.
If you’re still struggling, please repost your code in a way that is accurate and readable.
You are starting the animation and then immediately destroying the object. There will be no delay between those two lines of code (aside from whatever calculations are needed to setup the animator state). You need to actually wait for the animation to complete before destroying it. As a simple approach, consider looking into Animation Events. You cold also consider writing a State Machine Behaviour that the animation flows into after it completes. A couroutine could be used to delay the destruction of the object for a length of time could also be used.
There are many other ways that you could setup operations that happen over time to either check the current state of the animation or time it out in some way to know when to call destroy but that’s probably a little too much to worry about just yet.
Nevermind, I just realized you are using the delayed version of destroy. Time for more coffee
Although I do know what is wrong anyway. When you tell an animator to play an animation it does not update its internal state until the next animation update cycle of the frame you are in. This means the time you are passing as the delay is not the time of the animation you just set but rather the time of the last state just before you set it. You can manually ‘pump’ the animator by calling its Update() method just after setting the animation to play and it will then have the updated info.
Seriously, I think this is like the third or fourth post this week where this has come up lol