C
C#2y ago
hydraxic

Cannot access non-static method in static context?

I'm creating a JSON saving system for my unity game right now. I've already made one before that worked perfectly fine so I just copied that script over, but whenever I try to save data, I get "Cannot access non-static method 'SaveData' in static context" in Rider and Unity says "An object reference is required for the non-static field, method, or property 'JSONHandler.SaveData(int, int)'". The JSON saving script is not static and the script that it's getting called from is also not static. It also looks identical to the other game but does not work here.
12 Replies
boiled goose
boiled goose2y ago
but you have not created the object 😐 like
new JSONHandler().SaveData(...
new JSONHandler().SaveData(...
hydraxic
hydraxic2y ago
thats weird i didnt have to do that in the other game unless im just blind
TheRanger
TheRanger2y ago
ur method is probably static in the other game and besides this is Unity, you can't just use new on a class that inherits from Component you need to add the component to a gameobject first
hydraxic
hydraxic2y ago
it is on a gameobject tho
TheRanger
TheRanger2y ago
then access the JsonHandler that is attached to that gameobject
hydraxic
hydraxic2y ago
ive already done that
TheRanger
TheRanger2y ago
not according to your image
hydraxic
hydraxic2y ago
ok thats weird then ill try creating a new script and reattaching it
TheRanger
TheRanger2y ago
i dont see why you would do that you said its attached to that gameobject so just get the reference of that gameobject where is FinishGame defined?
hydraxic
hydraxic2y ago
i have no idea why but it works now it seems there was a problem with the file or something because i just copy pasted the code into a new script and it worked
TheRanger
TheRanger2y ago
you didn't modify anything?
hydraxic
hydraxic2y ago
no