So I have recently started my dive into Unity, almost exclusively 2D so far. However I’m having a bit of a bumpy start when it comes to trying to do things on my own rather than following a hand-holding tutorial. I am currently working on getting a few basic scripts going ( Character Controllers and very basic object interactions for things like damage).
Aside from the difficulties of getting the things to fully work, I am struggling to find where each given script should go. Is there any basic “rule of thumb” or “best practice” for where scripts should be attached or how they are organized on any given objects? I feel like I am really struggling with some basic concepts here and the “Where” of scripting seems like it would help get going on my own and working on the actual coding interactions.
Well, I don’t really know if there is some rules, but here are some of my practices:
Firstly, if you have written a script to a certain object, attach the script to this particular object.
Secondly, if you have scripts working as something like game handler, try to attach it to object which will not be interacted in any way (for example don’t let it be destroyed) such as empty object in background, or even camera if there is no interactions with it.
For me, most important thing is : don’t mix up the classes. Let ‘PlayerController’ only control player interactions and keep it out from handling other objects etc.
I hope this is at least a little helpfull. Cheers.
A lot of this is personal preference, and just what you think will work best for your game. For example, it is just as valid to place an AI control script on each individual monster object to control them individually, as it is to create some central monster controller script referencing every monster object and controlling them all from a single instance of the script. Often the former would be preferred, but if you have large numbers of monsters or do a lot of formation movements then the latter may be better for you.
As far as where to place scripts, just place them logically. If you have a UI window with buttons, it is probably a good idea to place the script the buttons call on the UI window instead of on some unrelated GameObject, as another example. It is also a good idea to break up large scripts that do a lot of largely unrelated tasks into smaller scripts that cover just a single topic. On the monster object example, the movement of a monster is probably a good idea to be in a separate script from the monster’s attack abilities, and again in a separate script from managing any inventory or loot drops when the monster dies. It could all be done in a single script, but it becomes harder to maintain 6 months after to wrote the code with a lot of barely related things in the same script.
Also, if you haven’t already, usually you create most of the objects in a game by instantiating prefabs rather than objects already built into the scene. I found early on that getting used to the idea of setting up GameObjects with their scripts and child objects that mostly take care of everything the object needs itself was a good way to go, especially when just learning. As another example, you fire a missile in your game, you just instantiate your missile object from a prefab, give it a reference to its target, and the missile object has everything it needs already attached to take care of itself.
(If you try out the new ECS system, you can throw out pretty much everything I said by the way, was referring to using MonoBehaviours, etc)
Thank you guys for the replies. I feel like keeping scripts sorted won’t be an issue for me (I have fairly picky and specific places I like for things to go… folders for everything)
@Joe-Censored your reply was especially helpful. I didn’t really think about scripts activating objects that they didn’t necessarily attatch to or interact with via colliders (such as a bullet doing damage to an enemy) but as far as my personal progress, I think I was 100% over thinking it. And even if I place a script in an odd place, it won’t truly matter except for organization. So thank you.
@TonyLi I am not quite there in my Unity journey, but thanks for the link. Once I get the beginner steps down, I will be looking into making better use of that.
Just a note that you can organize your scripts in Assets under folders differently than you organize their placement on objects if you find that benefits you.