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;
}
}
}