- Home /
An object reference is required to access non-static member
Hey all, I know this question pops up a lot, but I'm fairly new to Unity and my issue is a bit odd. I've been following the animation section of the stealth tutorial for use for my own project. The Animation controller itself is set up properly, but I'm getting errors in the script for some reason. Here's the code:
using UnityEngine;
using System.Collections;
public class EdelController : MonoBehaviour
{
public int locomotionState;
public int speedFloat;
public float turnSmoothing = 15f;
public float speedDampTime = 0.1f;
void Awake ()
{
locomotionState = Animator.StringToHash("Base Layer.Locomotion");
speedFloat = Animator.StringToHash("Speed");
Animator = GetComponent<Animator>();
Animator.SetLayerWeight(1, 1f);
}
void FixedUpdate ()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementManagement(h, v);
}
void MovementManagement(float horizontal, float vertical)
{
if (horizontal != 0f || vertical != 0f)
{
Rotating(horizontal, vertical);
Animator.SetFloat(speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
{
Animator.SetFloat(speedFloat, 0f);
}
}
void Rotating(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.MoveRotation(newRotation);
}
}
According to the errors such as:
Assets/Scripts/EdelController.cs(18,26): error CS0120: An object reference is required to access non-static member UnityEngine.Animator.SetLayerWeight(int, float)' Assets/Scripts/EdelController.cs(82,34): error CS0120: An object reference is required to access non-static member
UnityEngine.Animator.SetFloat(string, float)'
Assets/Scripts/EdelController.cs(86,34): error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'
The Culprit is in the MovementManagement function.
please help..
Answer by rutter · Dec 13, 2013 at 04:03 AM
Animator
is the name of a class. You might choose a different variable name, even if it's only different by virtue of capitalization.
//call "DoSomething" on an object of type Animator
Animator animator = GetComponent<Animator>();
animator.DoSomething();
//call a static function "DoSomethingElse" on the Animator class
Animator.DoSomethingElse();
Remember the difference between classes and objects? Classes are analogous to blueprints, objects are analogous to buildings built from those blueprints.
An instance member owned by an object, and is tracked per-object. Most variables are instance variables.
A static member is owned by the class, and is shared by all instances of the class. It's sometimes useful for data that should be shared throughout your game, such as a count or flag that persists between scenes. It's also useful for utility functions that don't necessarily reference any particular object.
Answer by KellyThomas · Dec 13, 2013 at 03:59 AM
The error is caused by an attempt to access the Class
, rather than an instance
. As a result it tries to find a static
property to revolve your request.
The solution is to change your code on lines 17-18 as follows:
Animator animator = GetComponent<Animator>(); //line 17
animator.SetLayerWeight(1, 1f); //line 18
You have similar issues in your MovementManagement()
method.