Created
November 6, 2018 11:59
-
-
Save NBaron/1e4dad3d9ae476b5c4363bb506f53d59 to your computer and use it in GitHub Desktop.
How to properly declare variables in a Unity component?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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