"An Object Reference is trying to access non-static member"

Im fairly new to scripting and instead of giving up i this time swore to myself to get Answers to make Questions in order to learn c#. That means if you do answer this Question, please explain it for me to understand and learn for the future :slight_smile:

My Problem heres is this Error: “An Object Reference is trying to access non-static member : UnityEngine.Animator.Play(string.int)”

and

My Problem heres is this Error: “An Object Reference is trying to access non-static member : UnityEngine.Animator.Play()”

With the Script i want to achieve playing different Animations on different inputs.

Here’s my Code:

using UnityEngine;
using System.Collections;

public class PlayerAnimations : MonoBehaviour {



	// Use this for initialization
	void Start () {
		GetComponent <Animation>();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKey(KeyCode.W))
			Animator.Play("Running");
	
		else if(Input.anyKey)
			Animation.Play("idle");
	}
}

you’re sort of on the right track - where you get the Animation component, you need to get the Animator component and store the reference for later use. something like:

_animator = GetComponent<Animator>();

define the variable like this:

private Animator _animator;

then later on something like:

_animator.Play("Running");