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 9083411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:46:01+00:00 2026-06-16T20:46:01+00:00

I have a class Unit that holds functions and variables that all Units in

  • 0

I have a class “Unit” that holds functions and variables that all Units in my game will share. I have then extended it with a “Soldier” class. Unit has a static variable that holds the base texture (as it will be the same for all units of that type when created, textures may change over time). These are loaded by:

Unit.baseTexture = content.Load<Texture2D>("worker");
Soldier.baseTexture = content.Load<Texture2D>("soldier");

When a “Unit” is created from the “Unit” constructor, it will load the texture with:

this.texture = Unit.baseTexture;

When a “Soldier” is created, it will load it like this:

this.texture = Soldier.baseTexture;

texture is a protected variable, not static so it should be one per object.

In the main game logic, I have an ArrayList that has multiple Unit and Soldier objects stored in it.

When I loop through them, I am doing:

foreach (Unit unit in unitList)
{
    unit.Draw(spriteBatch);
}

Draw is a function on the Unit class:

spriteBatch.Draw(this.texture, this.position, Color.White);

However, this is causing all units to draw with the last loaded texture (in this case, the soldier texture). This confuses me as I called the parent class first, then the child class. Why has loading the Soldier texture changed the Unit texture as well, if what gets drawn is a texture per object?

  • 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-16T20:46:02+00:00Added an answer on June 16, 2026 at 8:46 pm

    I think the correct thing to do here is use a property for BaseTexture, and then you can override it as needed, by using the override keyword. Use of the new keyword to hide members should be avoided.

    For example:

    class Unit
    {
       private static Texture2D s_unitTexture = content.Load<Texture2D>("worker");
       protected virtual Texture2D BaseTexture
       {
          get { return s_unitTexture; }
       }
    
       public Texture2D Texture { get; set; }
    
       public Unit()
       {
           this.Texture = BaseTexture;
       }
    
       ...
    }
    
    class Soldier : Unit
    {
       private static Texture2D s_soldierTexture = content.Load<Texture2D>("soldier");
       protected override Texture2D BaseTexture
       {
          get { return s_soldierTexture; }
       }
    
       ...
    }
    

    This way, when the constructor runs, the right BaseTexture for each type of Unit will be used.

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

Sidebar

Related Questions

I am creating unit tests with DUnit. I have a class that takes quite
We have a class that holds configuration information for the application. It used to
So I have this problem in PHP, I have a class called unit that
I have a class that holds on to a delegate, in order to lazily
I have a class I'm unit testing that requires fairly extensive database setup before
I have a class in a unit. Usually, when I changed the algorithm of
I have some classes(call it Class A) which I would like to unit test
I am writing some unit test and I have a unit testing base class
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is

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.