Challenge 3: Control the Animator from a C# script
Tutorial
·
Beginner
·
+0XP
·
10 mins
·
Unity Technologies

In this tutorial, you will create a basic C# script to control the animator state machine
Languages available:
1. Use the existing script to trigger various animations on the animator
1. With the DominoBlock GameObject selected, click Add Component and select the Animator State Changer script. Set the Animator reference and add the Trigger Name to the field

2. Press Play and use the “Spacebar” keypress to control the trigger.

3. Open the script in a code editor.

4. Notice the primary logic that makes this script function. The animator.SetTrigger can be used to set animator triggers. In this example a key press is used to start the set trigger sequence, but you could adjust this code to set the trigger after a certain time or when a certain event happens in your application.

5. To create multiple DominoBlocks in the scene, create an empty GameObject called Domino and add the DominoBlock as a child to it.

6. This makes the animation local to the parent GameObject which means you can clone the parent GameObject and move them around the scene and keep the animation functioning properly. Place 3 around the edge of the circle in preparation for the next steps.
