I'm trying to access some variables from different classes in one of my classes, I would like to be able to get the value and set the value of those variable from other classes.
Right now, I'm using static but I'm using it too much and I think that's not good practice.
So I'm trying to do with getters and setters but I can't make it work. Here is a small example of what I'm doing right now :
generalManager file
public float eggs ;
public float getEggs(){
return eggs ;
}
gameManager file
generalManager.getEggs() ;
And I have this error :
Assets/Scripts/gameManager.cs : error CS0120: An object reference is required for the non-static field, method, or property 'generalManager.eggs')
And I have to admit that I don't know what can I do to do not have this error anymore.
gameMaanager.csmake a reference forgeneralManager.csand assign it (If using Unity like it looks make a public reference and assign it in the inspector/editor)var myManager = new generalManager(); var eggs = generalManager.getEggs();As a side note: C# normally uses properties rather than bare getter/setter functions. Also StyleCop recommendations are to usePascalCaserather thancamelCasein C#.