Question by
Code1345 · Aug 11, 2017 at 05:59 PM ·
animation scriptjava to c#
A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent()'
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!
using UnityEngine;
using System.Collections;
public class Handgunreloading : MonoBehaviour {
public AudioSource ReloadSound;
public int ClipCount;
public int ReserveCount;
public int ReloadAvailable;
public Animator anim = GetComponent<Animator>();
void Update (){
ClipCount = GlobalAmmo.LoadedAmmo;
ReserveCount = GlobalAmmo.CurrentAmmo;
if (ReserveCount == 0) {
ReloadAvailable = 0;
}
else {
ReloadAvailable = 10 - ClipCount;
}
if(Input.GetButtonDown("Reload")) {
if (ReloadAvailable >= 1) {
if (ReserveCount <= ReloadAvailable) {
GlobalAmmo.LoadedAmmo += ReserveCount;
GlobalAmmo.CurrentAmmo -= ReserveCount;
ActionReload();
}
else {
GlobalAmmo.LoadedAmmo += ReloadAvailable;
GlobalAmmo.CurrentAmmo -= ReloadAvailable;
ActionReload();
}
}
}
}
void ActionReload (){
ReloadSound.Play();
anim.GetComponent<Animator>().Play("HandgunReload", -1, 0);
}
}
Comment