C
C#2y ago
Darkisad

❔ 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;
}
}
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;
}
}
9 Replies
ChucklesTheBeard
I bet if you were to separate those out into separate lines it would highlight the one with the issue.
sibber
sibber2y ago
also youre assigning to the locals oh wait you arent, the naming convention is flipped lol
Thinker
Thinker2y ago
This code looks fine, though it could be cleaned up a lot. As mentioned, it would help a lot with readability if you put all of the statements in the constructor on their own lines.
sibber
sibber2y ago
the line that errors isnt in the snippet
Darkisad
DarkisadOP2y ago
new Weapon("Sniper", 1, 8, 0.03f, 2.3f, SniperFireSound, SniperReloadSound)
new Weapon("Sniper", 1, 8, 0.03f, 2.3f, SniperFireSound, SniperReloadSound)
in this line error on "SniperFireSound" and "SniperReloadSound"
sibber
sibber2y ago
the error is for a field initializer so on a line that initializes a field
Darkisad
DarkisadOP2y ago
ik
Korbah
Korbah2y ago
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
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server