Hello, I am trying to write a script that lets me use a public variables to hold animation clips. I am getting the following errors when I try to call the variables and play the animation.
Here is the actual script (had some help on the script earlier to make it interchangeable in the first place, so that is why there are comments):
public class ContainerOpen : MonoBehaviour
{
public AnimationClip slideOutAnimation; // drag and drop this in the inspector
public AnimationClip slideInAnimation; // drag and drop this in the inspector
private bool open;
private float distance;
private GameObject player;
private Animation animation;
void Awake()
{
distance = 3;
open = false;
}
void Start()
{
// do the find once here
player = GameObject.Find ("FPSController");
if (player == null)
{
Debug.LogError ("Unable to find game object FPSController. Please fix and try again.");
}
// get a reference to the attached Animation component
animation = gameObject.GetComponent<Animation>();
if (animation == null)
{
Debug.Log("There is no Animation component attached to the current object. Adding one...");
animation = gameObject.AddComponent<Animation>();
}
// make sure the animation clips have been set
if (slideOutAnimation == null)
{
Debug.LogError ("slideOutAnimation was not set in the inspector. Please set and try again.");
}
if (slideInAnimation == null)
{
Debug.LogError ("slideInAnimation was not set in the inspector. Please set and try again.");
}
}
void OnMouseOver()
{
if ((player != null) && (slideOutAnimation != null) && (open == false))
{
if (Input.GetMouseButton (0))
{
Vector3 playerPosition = player.transform.position;
if (Vector3.Distance (playerPosition, transform.position) <= distance)
{
animation.Play (slideOutAnimation);
open = true;
}
}
}
if ((player != null) && (slideInAnimation != null) && (open == false))
{
if (Input.GetMouseButton (1))
{
Vector3 playerPosition = player.transform.position;
if (Vector3.Distance (playerPosition, transform.position) <= distance)
{
animation.Play (slideInAnimation);
open = false;
}
}
}
}
}