1
\$\begingroup\$

I have a game which creates an object in its Initialize() method:

    private GameComponent1 _thingy;
    protected override void Initialize() //Game1.Initialize()
    {
        base.Initialize();
        _thing = new GameComponent1(this);
    }

In the GameComponent1 class:

    public GameComponent1(Game1 game) : base(game)
    {
        _game = game;
        game.Components.Add(this);
    }

    public override void Initialize() //GameComponent1.Initialize()
    {
        //Do stuff here
        base.Initialize();
    }

However, in this case, _thing's Initialize method is not called. Why is this?

It behaves fine when the object is created in, say, Game1's Update method...

\$\endgroup\$
5
\$\begingroup\$

swap the order of this:

base.Initialize();
_thing = new GameComponent1(this);

to this:

_thing = new GameComponent1(this);
base.Initialize();

You need to initialize your components before, I believe.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Ah, silly me... How could I have missed this? /facedesk \$\endgroup\$ – Andrew Sun Mar 14 '13 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.