C#
C#

help

Root Question Message

Darkside
Darkside1/1/2023
❔ A field initializer cannot reference the nonstatic field, method, or property

class Weapon {
    public string name;
    public int ammo;
    public int damage;
    public float scatter;
    public float rate;
    public float reload;
    public AudioClip firesound;
    public AudioClip reloadsound;
    //constructor
    public Weapon(string _name, int _ammo, int _damage, float _scatter, float _rate, float _reload, AudioClip _firesound, AudioClip _reloadsound) {
        name = _name;
        ammo = _ammo;
        damage = _damage;
        scatter = _scatter;
        rate = _rate;
        reload = _reload;
        firesound = _firesound;
        reloadsound = _reloadsound;
    }   
}
ChucklesTheBeard
ChucklesTheBeard1/1/2023
I bet if you were to separate those out into separate lines it would highlight the one with the issue.
Cyberrex
Cyberrex1/1/2023
also youre assigning to the locals
Cyberrex
Cyberrex1/1/2023
oh wait you arent, the naming convention is flipped lol
thinker227
thinker2271/1/2023
This code looks fine, though it could be cleaned up a lot.
thinker227
thinker2271/1/2023
As mentioned, it would help a lot with readability if you put all of the statements in the constructor on their own lines.
Cyberrex
Cyberrex1/1/2023
the line that errors isnt in the snippet
Darkside
Darkside1/1/2023
new Weapon("Sniper", 1, 8, 0.03f, 2.3f, SniperFireSound, SniperReloadSound)

in this line error on "SniperFireSound" and "SniperReloadSound"
Cyberrex
Cyberrex1/1/2023
the error is for a field initializer
Cyberrex
Cyberrex1/1/2023
so on a line that initializes a field
Darkside
Darkside1/1/2023
ik
Korbah
Korbah1/2/2023
it looks like the error isn't in the weapon class but in whatever class is calling new Weapon("Sniper...) - the line of code I replied to
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy