Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Impossible access to C# struct elements

Discussion in 'Scripting' started by Ifridos, Jan 17, 2019.

  1. Ifridos

    Ifridos

    Joined:
    Jan 28, 2017
    Posts:
    11
    So I have this piece of code here, and I'm getting an error accessing to this struct vars because of its level of protection.
    I want to avoid making anything public if it is not completely necessary, and tried making these vars internal without success.
    I'm not an expert in C#, so I come here to ask for help. Any idea of how making this work?
    Code (CSharp):
    1. public class Clock : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     private struct GameTime
    5.     {
    6.         private static int day = 0;
    7.         private static int hour = 0;
    8.     }
    9.  
    10.     [SerializeField] private GameTime gameTime;
    11.  
    12.     public void SetDay(int day)
    13.     {
    14.         gameTime.day = day;
    15.     }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    1) day and hour are private. That means only members of 'GameTime' can access it. Make them public and then Clock can access it. Keep the GameTime struct private if you want to keep only Clock accessing it.

    2) day and hour are static. I'm going to fare a guess that they shouldn't necessarily be static.
     
    Ifridos likes this.
  3. Ifridos

    Ifridos

    Joined:
    Jan 28, 2017
    Posts:
    11
    True, they didn't need to be static, and thanks it's now working :)