Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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...

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
    
Ah, silly me... How could I have missed this? /facedesk –  Andrew Sun Mar 14 '13 at 9:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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