Search code examples
androidunity-game-enginemobile2d-games

Unity 3D Game Loop Logic and accessing data across scripts


I am a newbie to Unity3d. I am trying to develop an understanding of structure of a simple unity3d game.I have a basic game in which I have multiple objects on the screen & I have to count how many times user has tapped on them. Like I will have a total count of enemies killed. My question is what are the best practices to implement game loop & count things like total score. Can we have global variables that can be updated in all scripts or we need to pass data in between scripts or game objects.

Regards, Hassan Imtiaz


Solution

  • There is no straight answer to this, it depends on many angles with the architecture. I like the Event aggregation pattern, we use it in our game.

    You can subcribe to events by implementing IHandle when a message is published the Handle method on the instance will fire.

    In your case each time a enemy is killed Publish a EnemyKilled event. Your scoreboard can listen to this event and update