A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component

So, like the title says I am getting that error within my script. I am not exactly sure why, or what exactly it means. I think it will be a quick fix, and I have looked up answers, but none of them would work in this case I don’t think. I know that the problem is related to the Animators- I took the lines out for a moment and the script worked fine. And a side note- I am translating this from Javascript to c#. Thank you for any help!

This is also the entire error message, if you couldn’t see it:

A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent<UnityEngine.Animator>()’

1 Like
public Animator anim = GetComponent<Animator>();

You need to put this into Awake() or something.

1 Like

To clarify, you probably want Animator to stay global.

public Animator anim;

void Awake()
{
   anim = GetComponent<Animator>();
}

Then later on you’re doing

void ActionReload (){
ReloadSound.Play();
anim.GetComponent<Animator>().Play("HandgunReload", -1, 0);
}

//Should be able to just do

anim.Play("HandgunReload", -1, 0);

No need to retarget the Aimator, you already have a reference to it.

4 Likes

This worked great, thanks so much!

Hello from the future, can you explain why it must be in Awake or Start and can’t be done in the global space?

1 Like

You mean the line using GetComponent to assign to anim?

The class has not yet been created when the fields are being initialised, so there is no real meaningful way to let you run methods at that point. For example, if you:

public int myNumer = GetNumber();
public int myOtherNumber = 5;

public int GetNumber()
{
   return myOtherNumber;
}

What would you expect to happen?

1 Like

Yeah, it seems that GetComponent part won’t work anywhere else.

GetComponent is a regular C# method. You can use it wherever you can any other method. Awake, Start, Update, your own methods etc.

Ok, but why can’t it be done outside those as Code1345 had it initially?

Because reasons. Please don’t reply to four year old threads with silly questions. No matter what the reason is, you can’t do it.

Reason details: Unity does not run that part of the code (constructors) in a thread that Unity methods can be called from. Google Unity and the main thread for more questions.

If you have an actual legitimate question, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

2 Likes

Well, that’s kinda mean. I just wanted clarification as the original answerers explained what to do but not why as you just did.

Anyways, thanks. Sorry for bothering you. Happy New Year

1 Like

This is actually an error specific to C#, not Unity.

According to the C# spec, an instance of a class is created roughly like this:

  • the classes constructor is determined by overload resolution using the given arguments
  • field initializers for this class are called
  • if a constructor initializer is present (in the form of this(arg_list), base(arg_list), or the implicit base()) it is used to determine which base constructor to call – return to step 1 for this constructor call
  • the constructor’s body is executed

In other words, field initializers are executed before the constructor, and in a context before the object itself exists. This means that member methods of MonoBehaviour, such as GetComponent, which is an inherited member method from Component, are not available to be used. The object has not been created yet, so member methods cannot be used.

4 Likes

Ahhh I see, thanks!

1 Like