- Home /
Game structure - best object declaration practice?
Hi. I'm relatively a newbie when it comes to build apps in Unity, and i know there is some material / assets out there discussing best practices for game structure, but I wanted to get some opinion on best practices when it comes to declaring objects, datatypes and values when your setting up an app from scratch.
If i'm building something from scratch, my first instinct is to create and declare most objects I want to gain control of as public statics in a gamemaster.cs class and refer to those whenever needed in other scripts. Is this ideal?
i've read that functions like FindWithTag and GetChildren are taxing on performance, true?
Answer by Oana · Feb 14, 2017 at 08:54 AM
It depends based on your project, I don't think there is any way in which you should "always" do things. If it works and you can still read your code a few months later, it's fine. Just try to make it as clear and error-proof as possible.
Using "public static" can be ok. The problems arise if you end up having multiple objects of the same kind, or restarting the game (you need to make sure static values are reset when needed etc.)
Stuff like FindWithTag is rather expensive, particularly if you use it every frame in Update or something like that. But if you use it just once in Start or similar, it isn't really a problem.
Be careful if you use GetChild by index, because you may just end up modifying the order of stuff in the scene a bit and suddenly your code breaks. If there is any good "rule" to follow, it's to try to avoid "magic numbers" as much as possible.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i make my player stick with moving platforms? 1 Answer
3 person controler. rotate and turn 0 Answers
Need help with controller script. 1 Answer