- Home /
Where does C# game logic go?
Hello.
I'm having difficulty finding out where my Game Logic goes in Unity.
I understand that object-based AI, such as moving a game object across the screen over multiple frames, is done via attaching scripts to render objects.
But what about game logic that isn't tied to any particular object? Is there a "main()" or something where I can put code that orchestrates all visual objects, maintains game state, etc.?
Thanks, BT
Answer by QuestionBro · May 16, 2011 at 04:37 PM
You can just create an empty gameobject and simply place scripts on it to handle things such as GUI, game state, and anything else you might want to control(player stats or generation of the map, or even things like day and night cycles). Unless I am reading your question wrong is that along the lines of what you were wondering?
So create an empty object in the scene, attach my GameLogic() class/script to it, then do all game logic from within its Update() function?
Typically what goes in such a class is overall game state for game save purposes, etc. It would handle anything that's more of a "global" event, such as creating new bugs at the top of the screen in Centipede. Once those things are created they run on their own, but only a higher-level intelligence knows WHEN to create them.
Yes that is essentially what you do, you do not need to put all the logic in one scripts update, you can split things up into manageable classes, but using empty gameobjects for controlling basic functions in your game such as states is fine. I usually see in some of the larger projects I have worked on / seen have a "general scripts" empty gameobject in them for just such a purpose. Hope that helped ya out.
Yep, thanks! Will just take some getting used to. We have some legacy C++ game logic as well as some device drivers we must communicate with, so it will be interesting. Right now I'm just trying to nail down to workflow to ensure feasibility.