Animation for Powerup Won't Work?

Hey guys, it’s been awhile since I’ve ask a question. I’m having a problem with my script that supposed to play an animation after I grab a power up. It won’t work and I can’t figure out why.

public class AnimTest : MonoBehaviour {

	GameObject BoostPowerupSpawn; 
	
	void  OnTriggerEnter2D ( Collider2D Other  ){
		
		if(Other.gameObject.tag == "Player"){
			
			BoostPowerupSpawn.animation.Play("Boost");
		}
	}
}

“BoostPowerupSpawn” is the name of the gameobject for the power up. “Player” is the name of the tag for the player/character. “Boost” is the animation I want to play. Both the power up and the player have animator attached to them. When I put this script on my power up, it looks like this. When I tried to put it on my player, the BoostPowerupSpawn changes to Character, the tag Player changes to Item, and the name of the animation changes to “Death”(since I don’t have a boost animation attached to the player yet because this is a test). Please help me. By the way, “Boost” is not in the animator, but I don’t see how that affects anything since Death is in the animator yet that doesn’t work.

27951-characteranim.png

Hey, i’m not too knowledgeable on mecanim but I might be able to help with one or two things.

First thing I noticed:
You said your death animation doesn’t play? It’ll be because there is no transition to it.
Create a transition from ‘Any State’ to your ‘death’ state

I’m not sure at all weather you can use animations as well asing animators… however I don’t see why you’d want to. You could also transition from ‘Any State’ or something to ‘Boost’

and you could set it to a bool e.g. “boost” = false; *In the animator panel
Then in script you would access it like this:

anim.SetBool ( “boost” , true);

anim being a reference to the Animator Component on the object

Which can be found, and initialised in the awake function

e.g.

Animator anim = GameObject.FindObjectWithTag(“Player”).GetComponent();

That being said, i’ve never used 2D either, so maybe its different.
Either way, give it a try and good luck

Also: All code is untested just off the top of my head, there may be spelling errors.