Now we all know there is no right way to code, although many of us have a general format we try to use. I was wondering what you all consider acceptable amoung programming in unity. I’m hoping experienced people will provide their input on writing efficient clean code, and new people will learn something from this.
Firstly regarding scripts I was taught a script should only affect the object its attached to it and its children. If you needed a script that managed over several objects such as a game manager for instance you create that script and make variables for each script you need to access. For example, a GameManager might be seen with a UIManager variable, PlayerScript variable, and etc… Also always name your scripts relative to their purpose and or object they are on for example you have a script responsible for moving the player you would call this “PlayerController” or “PlayerMover”. And scripts that “manage” over several other objects would be things like “GameManager”, “UIManager”, “WorldManager” these managers would also commonly be singletons.
Next, there are variables, I was taught to always declare whether variables are public, protected or private and to always assign their get/set conditions. Regarding naming variables always use cammelCase. Also, take advantage of attributes for example if you need to input a big string. Use the [TextArea] attribute. or if you have integers and floats you need your designer to mess around with, use the [Range(min, max)] attribute.
Now for the biggest debate functions. The number one rule I’ve been taught is to keep functions single purpose. They should do one thing and one thing only. If a function is adding experience to the player for killing a monster it should not check if the player can use an ability from a cooldown. Other than that I don’t have much to comment on with functions. Remember to look for the monobehaviour base functions there are many good ones such as OnApplicationQuit(), OnClick(), etc.
I look forward to hearing what you all have to say regarding scripting sorry to those javascript scritors I only use c# so I used those terms.
PS: Some shameless self-promotion I am looking to join a team who needs a programmer I’ve been using Unity for 4 years have released an app entirely by myself. I do free volunteer work and would love a paid job if any takers ![]()
Finally using everything I said here is my example of a “perfect” gun script. Feel free to leave your comments on this ![]()
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class WeaponController : MonoBehaviour
{
//Attaches to a child object of the object this script is on.
public ParticleSystem C_muzzleFlash;
//Attaches to the same object this script sits on
public AudioSource C_gunShot;
[Range(0.05f, 1.00f)]
public float V_coolDown = 0.01f;
public float V_nextFire {get; private set;} //I would normally keep this variable private but I did it this way to demonstrate the get/set stuff I mentioned.
/*I use the start function to verify and assign any variables I need to do at the start of the game.
//This seems redundant and uncessary but is actually very good information to check. Otherwise you get the error
"unity object reference is not set to an instance of an object" and I think my wording makes more sense than that.*/
void Start()
{
if(GetComponentInChildren<ParticleSystem>())
{
C_muzzleFlash = GetComponentInChildren<ParticleSystem>();
}
else
{
Debug.LogError("No Particle System found in child objects");
}
//Although I have the RequireComponent attribute at the top I still double check it as a double measure.
if(GetComponent<AudioSource>())
{
C_gunShot = GetComponent<AudioSource>();
}
else
{
Debug.LogError("No Audio Source found on object");
}
}
void Update()
{
//I don't check to see if the axis exists because A. this is a default axis included when you make a unity project and unity has its own logerror which explicatly states if you spell a axis wrong.
if(Input.GetAxis("Fire1") > 0 && V_nextFire < Time.time)
{
Shoot();
}
}
//This is an example of a single purpose function
void Shoot()
{
V_nextFire = Time.time + V_coolDown;
C_muzzleFlash.Play();
C_gunShot.Play();
//I'm not gonna include the whole raycasting part because its been done a million times and its 11pm and I need sleep...
}
}
//Final note Include lots and lots of comments in your scripts. WEEEEEEEEEEEEEEEEEE!!!