Using Content.Load <> in a class / subclass
I am wondering if, in any case, using "Content.Load <>" in a class that is not the game itself, I will say that I want to load the texture from the class, and not send the texture to it.
namespace ProjectGame1 { public class Ship { public Texture2D texture; public Ship() { this.texture = Content.Load<Texture2D>("ship"); } } } This is an example of what I'm trying to achieve.
Firstly, I recommend not using DrawableGameComponent , my arguments for this are described in this answer here .
Now, to make your code work as is, you need to pass the ContentManager to the constructor you are creating (see JoDG answer ). But for this you should build it only after the ContentManager ready. For the Game content manager, this happens during and after the LoadContent , which is called (and not in your game counter constructor or Initialize method).
Now you can do something like using DrawableGameComponent, which is much nicer: just give your Ship class a LoadContent method and call it from your LoadContent game (as you would for Draw and Update ).
If the texture your ship uses is not part of your vessel state (i.e. all ships use the same texture), you can even make it static, eliminating the need to call LoadContent on every ship you create. I have an example of this this answer here , which also has a list of other useful information about Content Manager.
For this to work, you will need a Constructor that takes a parameter of type Game . This will mean that you need to add a link to Microsoft.Xna.Framework.Game .
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; namespace ProjectGame1 { public class Ship : DrawableGameComponent // Notice the class now inherits from { // DrawableGameComponent public Texture2D texture; public Ship(Game game) : base(game) // <<---- Don't forget to pass Game to the base constructor { this.texture = game.Content.Load<Texture2D>("ship"); } } }