- Home /
How to trigger animation from another game object that has it's own script.
I'm trying to trigger the animation from the Sword Script from the player's script by pressing W. This is the current Sword script. Thank you in advance.
Animator animator;
void Start() {
}
public void PerformAttack()
{
animator.SetTrigger("Base_Attack");
}
void OnTriggerEnter(Collider col) {
animator = GetComponent<Animator>();
if (col.tag == "Enemy") {
col.GetComponent<IEnemy>().TakeDamage(30);}
}
Answer by tormentoarmagedoom · May 10, 2018 at 11:04 AM
Good day.
This is a very very basic code issue. You need to know how to refear a component of another object.
Create a public variable, save the script, and attacj it via unity inspector. Then can easy acces that object components.
You should read/watch some manuals/tutorials about this basic issue: Reference objects from scene to scripts, and reference objects from script to script.
I'm still having trouble. Would it be much to ask for an example?
Your answer
![](https://koobas.hobune.stream/wayback/20220612162359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
run animation when should idle 1 Answer
Inputs and animations interfering with each other 1 Answer
hash does not exist error 1 Answer