|
I have started programming c# just for a few days and i learned a bit about variables and that sort of stuff. now i have this script: using UnityEngine; using System.Collections; and i have the errors: Assets/Scripts/AutoAttack.cs(9,76): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `AutoAttack.maxHealth' Assets/Scripts/AutoAttack.cs(10,62): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `AutoAttack.loadingBarLength' i'm sorry for being souch a noob, because it will probably be a simple declaration i got wrong but i can't quite seem to find it.
(comments are locked)
|
|
In C# you can't define the initialization of variable interms of another variable.
(comments are locked)
|
|
Basically, you can't define your initializers in terms of each other. Outside of any function calls, that stuff doesn't actually execute as such- it merely defines the starting state for the rest of the script. If you want to do calculations like that on your initial values, you should do it all in the 'Start' method, which gets called at the very beginning of the game. Now, since 'curHealth' sounds like the kind of thing that would change at runtime, you should also change loadingBarLength inside of Update so that it always properly reflects how much health your character has. Now, a better question is- Why is it called 'loading bar' when it's obviously used for current hit points? You should make sure you give your variables accurate names, or it'll confuse the hell out of you in pretty short order. Thank you a lot, and why it's named that way is because im just testin and trying to learn, not to actualy make something out of it. but your help is very much appreciated ^^
Nov 17 '11 at 02:11 PM
runetimon
It is good practice to name your variables something relevant too, so start doing that.
Nov 22 '12 at 07:55 AM
Frix
(comments are locked)
|
