Skip to content

Instantly share code, notes, and snippets.

@NBaron
Created November 6, 2018 11:59
Show Gist options
  • Save NBaron/1e4dad3d9ae476b5c4363bb506f53d59 to your computer and use it in GitHub Desktop.
Save NBaron/1e4dad3d9ae476b5c4363bb506f53d59 to your computer and use it in GitHub Desktop.
How to properly declare variables in a Unity component?
using System;
using UnityEngine;
namespace awesomeCompanyName.awesomeProductName.relevantNaming
{
public class MyComponent : MonoBehaviour
{
private OtherComponent _usedOnlyByMe;
[SerializeField]
[Tooltip("Explicit description of what I am.")]
private OtherComponent _setInInspectorAndUsedOnlyByMe;
/// <summary>
/// Explicit description of what I am.
/// </summary>
public OtherComponent ReadOnlyByOthers => _setInInspectorAndUsedOnlyByMe;
/// <summary>
/// Explicit description of what I am.
/// </summary>
public OtherComponent ReadWriteByOthers
{
get { return _setInInspectorAndUsedOnlyByMe; }
set { _setInInspectorAndUsedOnlyByMe = value; }
}
public OtherComponent DontDoThis; // Never!
private void Awake()
{
// Check required fields.
if (!_setInInspectorAndUsedOnlyByMe)
throw new InvalidOperationException("Explicit message about what is missing.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment