- Home /
error cs0236 field initializer cannot reference a nonestatic field, method or property
public class UnitGoal : MonoBehaviour {
BrainHealth BH = gameObject.GetComponent<BrainHealth>();
void OnTriggerEnter(Collider c) {
Destroy(c.gameObject);
}
}
please help
Comment
Answer by tanoshimi · Jun 26, 2014 at 12:47 PM
Like the error message says, you're trying to initialise BH with a value returned from GetComponent(), which is not static.
public class UnitGoal : MonoBehaviour {
BrainHealth BH;
void Start() {
BH = gameObject.GetComponent<BrainHealth>();
}
void OnTriggerEnter(Collider c) {
Destroy(c.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Error NullReferenceException: Object reference not set to an instance of an object 1 Answer
OnDestroy() : Some objects were not cleaned up when closing the scene 1 Answer
Getting ArgumentOutOfRangeException on array c# 1 Answer
Need help calling a script to another keep getting errors 1 Answer