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\$

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .