Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 24, 2016 at 08:30 PM by Vexing for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Vexing · Jul 24, 2016 at 07:55 PM · referenceinstancetypestatic variablestatic class

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Salmjak · Jul 24, 2016 at 08:04 PM 1
Share

@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.)

avatar image Vexing Salmjak · Jul 24, 2016 at 08:13 PM 0
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference a static variable from an instance 2 Answers

The type or namespace name `GridSelection' could not be found. Are you missing a using directive or an assembly reference? 1 Answer

Creating a data type instance difference question 1 Answer

Object reference not set to an instance of an object 1 Answer

Object reference not working 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges