UserBruiser
UserBruiser

Reputation: 155

Changing values in a struct?

I am saving game data in my XNA C# Windows game and I've come across a problem (due to my lack of knowledge).

I've created a struct outside my public class Game1 as follows

public struct SaveGameData
    {
        public string PlayerName;
        public int Score;
    }

Then inside the main method (public class Game1)

SaveGameData saveGameData = new SaveGameData()
        {
            PlayerName = "Jimmy",
            Score = 100,
        };

I can't access PlayerName elsewhere in my code so I thought I could set PlayerName = to another variable such as "string name". But I get the following error message

a field initializer cannot reference the nonstatic field, method or property

Is there a way of changing these values dynamically? Or am I going about it the wrong way?

EDIT

Okay sorry for the lack of information provided.

I was declaring two variables

public String name;
public int score; //small s

Then I was trying to set PlayerName = name and Score = score but I was getting the above error.

By taking Wimmel's advice, I changed the fields to static and that is gotten rid of the error.

However, in my Update method, when I update the score value...the value of Score is not updating also. In my .txt file that I am outputting to, the Score value is the initial value of "score"

Upvotes: 0

Views: 234

Answers (1)

wimh
wimh

Reputation: 15232

You are trying to access SaveGameData which is just the type. You must change saveGameData or make the fields static.

Upvotes: 4

Related Questions