Why is my Animator not being activated by my UI Buttons?

I am working on a menu system that involves swiping left or right to navigate and includes redundant button controls. The menu functions as follows:

  • The appropriate input activates the animator attached to the Panel being moved which slides it neatly off the screen to one side.
  • The Panel is deleted at the end of the animation.
  • A new item is instantiated.

The swipe version of the controls work just fine, but my UI.Buttons are not animating the the panel. I keep getting a Warning: “Animator has not been initialized. UnityEngine.Animator:SetFloat(String, Single).” Both the Swipe input and the Buttons make use of the same function which is in a script attached to the panel instance:

public void SwipeItem(float swipeDirection)
	{
		// Activate Animator
		GetComponent<Animator>().enabled = true;
		if(GetComponent<Animator>().enabled)
		{
			// Set State Change Parameters
			GetComponent<Animator>().SetFloat("MouseXPosition", swipeDirection);
		}
	}

As of the writing of this question I have actually solved this problem with some insight from my boss.

When attaching scripts to UI Buttons you should not use functions that are attached to prefabs that are being instantiated at runtime. The button was trying to activate the script as it was attached to the un-instantiated prefab, not the instance that needed to be moved.

I ended up using my swipeControl script to access the SwipeItem(float) function attached to the currently instantiated Panel.

public void ButtonClicked(int swipeDirection)
	{
		m_previousItem.GetComponent<DragPanel>().SwipeItem(swipeDirection);
	}