- Home /
setTrigger() for another GameObject doesn't work
I have a Game Controller on a Game Controller object and I want to setTrigger(); on the players (another object) animator to trigger his animation.
This doesn't work. The following is my code:
public class buttonSwap : MonoBehaviour
{
[SerializeField]
private GameObject player;
private Animator playerAnim;
private void Awake()
{
playerAnim = player.GetComponent<Animator>();
}
// Start is called before the first frame update
void Start()
{
playerAnim.SetTrigger("keyPressShape");
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
playerAnim.SetTrigger("keyPressShape");
}
}
Answer by rob5300 · Jan 22, 2019 at 11:00 AM
You need to check that the trigger is spelt correctly in the Animation Controller and that a transition is set to use the correct trigger. Have it open during play mode to see if it transitions to the next node.
all spelt correctly and it's even working when I use the command inside a script attached to the player Game Object (and change playerAnim.SetTrigger();
to gameObject.GetComponent<Animator>().SetTrigger();
). If I use it in a script attached to the Game Controller Object it doesn't work anymore.
Put a break point (or use Debug.Log) on where you set the trigger to see if it actually is called. Are you sure that player is not null?
also did I used print("test");
and it got called.. player is not null because I tried it like this (and assigned a player) but also with GameObject.Find("Player")
and so on.. none of that worked sadly and I really don't see why
Your answer
Follow this Question
Related Questions
Repeat a Animation on the same GameObject? 1 Answer
How do i destroy only 1 object when 2 instances of the same object collide ? 2 Answers
triggering random animations with gui 1 Answer
Mechanim Trigger Issue 1 Answer
moving my wall up & down 2 Answers