(yet another) error CS0120: An object reference is required to access non-static member
OK, don't be one of those dutchbags and say 'I went to google and found...' I did and found an endless loop of errors but none that helped me. I know what the issue is, but can't code it right. this should be pretty simple for you to answer.
On script #1 (UpdateStats) I have a variable "public string sceneStudyMode;". In the inspector I type in a name.
On script #2 (PlayerPrefsSTudy), I want to access it. I tried "UpdateStats.sceneStudyMode" and I get the error.
////////////////////////////
/// Study Mode
if (PlayerPrefs.HasKey (UpdateStats.ppText + UpdateStats.sceneStudyMode + UpdateStats.victoriousText))
{
studyModeVictory = PlayerPrefs.GetFloat (UpdateStats.ppText + UpdateStats.sceneStudyMode + UpdateStats.victoriousText); //Load
}
else
{
studyModeVictory = 0;
}
//////////////////////////
I think that it has something to do with referencing. I know what needs t be done. The variable is not in a method.
Thanks for the help and not disrespecting me.
I moved the question to the Help Room as this is not a Unity specific question. This is a very basic program$$anonymous$$g question and as such doesn't belong to the default space. Please read the FAQs and the User guide.
Also you should read some basic OOP books / tutorials as you seem to to not understand the fundamental concept of objects, instances and classes.
Answer by toromano · Jun 17, 2016 at 06:53 AM
You need to get an instance of UpdateStats and access that instance's sceneStudyMode member. Or you should make sceneStudyMode static to access from anywhere.
If you need only one instance and access from anywhere freely you might wanna check Singleton pattern out : http://wiki.unity3d.com/index.php/Singleton
"You need to get an instance of UpdateStats and access that instance's sceneStudy$$anonymous$$ode member"
great, but how does one code that?
Answer by tanoshimi · Jun 17, 2016 at 07:58 AM
Like the message says, you need an object refetence So add one at the top of script 2:
public UpdateStats updateStats;
Assign the UpdateStats component to that variable by dragging it into the slot in the inspector. And then use that object by replacing all your exsiting references to "UpdateStats.victoriousText" (the class) etc. to "updateStats.victoriousText" (the instance of the class as dragged into the inspector)
The updatestats game object and script are in another scene. So how would I drag it into the slot in the inspector?
I see what you mean here in the code with "public UpdateStats updateStats;". So I use the class name and another name. Then here just use the other name as a reference. However, the script is on a game object in another scene.