An Object Reference is Required to Access Non-Static Member

I am trying to make a first-person survival game, but while I was animating the arms I got an error that said “An object reference is required to access non-static member.” I see that this is an error people get often, but I can’t find anyone who had the error happen when using Animation.Play. (I get the error on every line where I use "Animation.Play(“Animation”);
Here is my code:

using UnityEngine;
using System.Collections;

public class ArmController : MonoBehaviour {

	private CharacterController controller;
	private GameObject Player;
	private bool canSwing;
	private bool isSwinging;
	private bool hasAxe;
	public float swingTimer;
	private GameObject Arms;

	// Use this for initialization
	void Start () {
		Player = GameObject.Find ("FPSPlayer");
		controller = Player.GetComponent<CharacterController>();
		swingTimer = 1;
		Arms = GameObject.Find("FPSPlayer/FPSArms_Axe@Idle");
	}
	
	// Update is called once per frame
	void Update () {

		if (controller.velocity.magnitude <= 0 && isSwinging == false) 
		{
			Animation.Play ("Idle");
			Animation ["Idle"].wrapMode = WrapMode.Loop;
			Animation ["Idle"].speed = 0.2;
		}

		if (controller.velocity.magnitude > 0 && Input.GetKey (KeyCode.LeftShift)) 
		{
			Animation.Play ("Sprint");
			Animation ["Sprint"].wrapMode = WrapMode.Loop;
		}

		if (hasAxe == true && canSwing == true)
		{
			if (Input.GetMouseButtonDown (0)) 
			{
				Animation.Play ("Swing");
				Animation ["Swing"].speed = 2;
				isSwinging = true;
				canSwing = false;
			}
		}
		if (canSwing = false)
		{
			swingTimer -= Time.deltaTime;
		}

		if (swingTimer <= 0) 
		{
			swingTimer = 1;
			canSwing = true;
			isSwinging = false;
		}
	
	}
}

Hi there @lucas216erickson

The error you are receiving is due to you using the global reference to the Animation class rather than a reference to your specific Animation. Unless a class, field or method is static you cannot access it without an appropriate reference. So to fix your error you will simply need to create a reference to your animation and then replace all instances of “Animation” in your code with your new animation reference. For example:

public class ArmsController : MonoBehaviour
{
	private CharacterController controller;
	private GameObject Player;
	private bool canSwing;
	private bool isSwinging;
	private bool hasAxe;
	public float swingTimer;
	private GameObject Arms;

	// Reference to our animation which we can assign in the inspector.
	public Animation AnimationRef;

	// Update is called once per frame
	void Update () 
	{
		if (controller.velocity.magnitude <= 0 && isSwinging == false) 
		{
			// Old code
			//Animation.Play ("Idle");

			// Old code replaced with reference.
			AnimationRef.Play ("Idle");
			AnimationRef ["Idle"].wrapMode = WrapMode.Loop;
			AnimationRef ["Idle"].speed = 0.2;
		}
	}
}

I hope this helps! :slight_smile:

Ok, it works, thanks!