- Home /
The question is answered, right answer was accepted
Static member cannot be accessed
So, I've had this problem for a few days now and I can't figure out what I'm doing wrong here.
I keep getting the error:
"Static member `DaySave.playtime' cannot be accessed with an instance reference, qualify it with a type name instead"
I don't think I'm using an instance reference to get this value but I could be horribly wrong. This is the code that is giving me the error CS0176:
void Update ()
{
foreach(DaySave g in SaveLoad.savedDays)
{
if(g == SaveLoad.savedDays[0])
{
DaySave.current = g;
float hour = DaySave.current.playtime / 3600;
float minutes = (DaySave.current.playtime % 3600) / 60;
float finalHour = Mathf.Floor(hour);
float finalMinutes = Mathf.Floor(minutes);
Debug.Log(finalHour.ToString() + " : " + finalMinutes.ToString());
}
}
}
Here's the other classes that I'm using as well:
[System.Serializable]
public class DaySave
{
public static DaySave current;
public static string saveName;
public static int day;
public static int tod;
public static float playtime;
public DaySave()
{
saveName = "New Save";
day = new int();
tod = new int();
playtime = new float();
}
}
And
public static class SaveLoad
{
public static List<DaySave> savedDays = new List<DaySave>();
public static void Save()
{
SaveLoad.savedDays.Add(DaySave.current);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/savedDays.dex");
bf.Serialize(file, SaveLoad.savedDays);
file.Close();
}
public static void Load()
{
if (File.Exists(Application.persistentDataPath + "/savedDays.dex"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedDays.dex", FileMode.Open);
SaveLoad.savedDays = (List<DaySave>)bf.Deserialize(file);
file.Close();
}
}
}
I'm getting to the point where I feel I need to just walk away for a few days or something, it's really bothering me. Thanks for the help!
@Vexing You don't seem to understand what "static" does because all your variables are static yet you have a list of instances of the class...
I like to see static variables as shared variables between instances of the class. Or rather, a static variable belongs to the class itself and not instances of the class. Look up the definition.
Let's say we have static string name = "Gabriel";
Now we create two instances of the class Person: Person individual_A = new Person(); Person individual_B = new Person();
now we change name = "$$anonymous$$ikael";
Both individual_A and individual_B has the variable name = "$$anonymous$$ikael". (Or rather, neither have it at all. Because it doesn't belong to them in the first place but to the class itself, thus your error.)
I'm pretty much using static classes and variables for the first time, so that's a fair assessment.
So I just need to make my variables not static? I feel pretty silly, I think I was just on auto-pilot when writing those.
That's the problem, Thanks a lot! A healthy dose of embarrassment is good every now and then.