title: Why can't I control an Animator using a UI Button?
body: <p>I'm very much a Unity/C# beginner, and I've been working on an Android app that has some UI buttons that are supposed to play, pause and reset an animation on <code>OnClick()</code>. And despite my best efforts, I can't get the UI buttons to control the animator in my project. What's especially frustrating is that I was able to control an animator in a very similar project when using key presses as the trigger instead. So it's something about this other project or the way I'm calling the animator that is causing the problem.</p>
  <p>Here's the setup:</p>
  <p>My object to be animated has my C# script and Animator attached to it. It may be worth noting that the object isn't placed directly into the scene, but is contained within a prefab that is later instantiated (and that part works fine).</p>
  <p><a href="https://i.stack.imgur.com/rjkbP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rjkbP.jpg" alt="screenshot of components on animating object" /></a></p>
  <p><a href="https://i.stack.imgur.com/BVSCc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BVSCc.jpg" alt="screenshot of animator setup" /></a></p>
  <p><a href="https://i.stack.imgur.com/9QJyz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9QJyz.jpg" alt="screenshot of button onclick example (all 3 buttons are set up like this with their respective functions called)" /></a></p>
  <p>I then have the following code:</p>
  <pre class="lang-cs prettyprint-override"><code>public class AnimationControls : MonoBehaviour
  {
      private Animator anim;
  
      public Button uiplayButton;
      public Button uipauseButton;
      public Button uiresetButton;
  
  
      public void Start()
      {
          anim = GetComponent&lt;Animator&gt;();
      }
  
      public void Pause()
      {
          Debug.Log(&quot;You have clicked the pause button!&quot;);
          anim.Play(&quot;pauseAnimation&quot;);
      }
  
  
      public void Play()
      {
          Debug.Log(&quot;You have clicked the play button!&quot;);
          anim.Play(&quot;playAnimation&quot;);
      }
  
      public void Reset() 
      {
          Debug.Log(&quot;You have clicked the button!&quot;);
          anim.Play(&quot;resetAnimation&quot;);
      }
  }
  </code></pre>
  <p>When I build and run this on my phone, pressing the buttons shows the correct debug message, but the animations do not change. I've tried other methods of triggering the animations as well (triggers/booleans linked to transition states etc.) and each time, nothing happens except the debug message. So what I've shared here is the simplest version of the code I've used.</p>
  <p>I can also confirm that my animations are set up correctly, as if I set the playAnimation state to the default, it plays immediately on startup. And the animation stays paused when pauseAnimation is the default.</p>
  <p>So, what am I missing here? What do I need to change to get the script to successfully target the Animator and switch between the animations when the buttons are clicked? If any further clarity is needed please let me know.</p>
  <p>Edit: Updating this post to add a screenshot of current transition settings, below:</p>
  <p><a href="https://i.stack.imgur.com/ic54U.jpg" rel="nofollow noreferrer">screenshot of Animator transition settings</a></p>
  <p>Previously I had Conditions set for the various transitions, I've tried using booleans and triggers to activate the transitions via C# and those didn't work. I also tried Exit Time but that just made the transitions happen automatically which isn't what I want. So now I've got it like this.</p>
  <p>Edit 07/10/20: I solved it!</p>
  <p>The problem was the way I was trying to identify the prefab and its animator. I had declared it in my script and specified the prefab in the Inspector (as shown above), but I don't think that was matching up with the instantiated version of the prefab that appears in game. And because of that, my methods were not able to address the correct AnimatorController.<br /><br /></p>
  <p>So the solution was to go into the AR Controller code where the gameObject itself gets instantiated, call the Animator there, and put my methods afterward.<br /><br /></p>
  <p>This was the critical code that made it work, where gameObject refers directly to the instantiated version of the prefab in the AR Controller script:</p>
  <pre><code>anim = gameObject.GetComponentInChildren&lt;Animator&gt;();
  </code></pre>
  <br />
  After this, any attempts to address the Animator via methods, buttons etc. work as intended.<br /><br />
  <p>Hope this solution helps someone in the future as I went through literally dozens of forum posts trying to solve this.</p>