Accessing class properties that have been instantiated within another class

I have two custom classes. The TEAM class has an array which has an array that contains many instances of the PLAYER class. For example:

home = new Team();
home.player[1] = new Player();
home.player[2] = new Player();

When I define players using this setup, I can’t seem to access their properties:

e.g. home.player[1].x = 100;

leads to an error (“Null object reference”).

Is there a better way to do this that I’m missing? Is there some property of the team/player class that I need to set?

How is the player array defined? Does it does use Dynamic, or is it an Array<Player>?

Is it possible that your code is running before player[1] has been set?

Perhaps you could try checking before updating the x

trace (home.players.length);

for (player in home.players) {
    
    trace (player);
    trace (player.x);
    
}

Ahh - I had originally defined the array as dynamic - switching it to player did the job. Thanks!

Sprite (and other classes in OpenFL) use getter/setter functions for fields such as x and y. When you compile, sprite.x = 100 really becomes sprite.set_x (100) under-the-hood, but only if Haxe knows the type :slight_smile: