Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:35:54+00:00 2026-06-15T05:35:54+00:00

I am saving game data in my XNA C# Windows game and I’ve come

  • 0

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”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:35:56+00:00Added an answer on June 15, 2026 at 5:35 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a game write now which involves saving highscores and basic data such
I've been investigating alternative methods for saving my game's data between turns, and wonder
I am playing around with saving .xml files from data in my game. I
I'm making a simple 2 player game in XNA and started looking into saving
I'm just getting into saving data from my game into a .plist, but I'm
I'm writing a game using Surfaceview and have a question relating to saving Data
I'm trying to create a game save system which involves saving a score for
When saving a new entity something strange happens... I have a data grid with
between saving a key name like this for(var key in this.data) { var key_name
I'm saving a struct into a .dat file. Lets suppose I have to edit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.