Unity 2018 - Using SerialilzeField to expose private variables

The most common approach to exposing variables in the editor is to make them publicly available. This approach is great for values that need to be tweaked without digging through the code, but can cause problems when referencing variables that need to remain private. A common use is using the Unity pattern of assigning prefab references by dragging them from the editor to the exposed variable slot on a script. This makes assigning references easy, but by making this variable public, the values can be changed/accessed programmatically from other scripts. This is fine for updating simple strings and integers, but can cause some undesirable effects when a prefab reference is changed. For example:

using UnityEngine;
using System.Collections;

public class PlayerClass : MonoBehavior {

	public int playerHealth = 0; // <---- Fine to change in editor
	public GameObject player; // <---- Can be changed by code

	void Update() {
		Player.doSomething(); // <---- This can break if Player prefab is swapped for another
		...
	}
	
}

As shown above, if the Player prefab is accidentally swapped out with another, the methods on Player may not exist on the new prefab, which will break the code. This is just one basic side effect, lots of other crazy stuff could occur.

The “best practice approach” would be to to use [SerializeField] to expose a private variable to the editor.

using UnityEngine;
using  System.Collections;

public class PlayerClass : MonoBehavior {

	public int playerHealth = 0;

	// public GameObject Player;
	[SerializeField] private  GameObject  playerPrefab;
	private  GameObject _player

	void Update() {
		_player = Instantiate(playerPrefab) as  GameObject;
		...
	}
}

In this example the playerPrefab reference variable is exposed to the editor, but remains private. As an added level of protection, the prefab reference is being stored in it’s own variable and the instance of the prefab is being stored in another. This is a great pattern for instantiating a GameObject.

For more information on [SerializeField] check out the Unity Reference

Thanks for reading! -CL

Published 20 Dec 2018

General Consultant To All Fields of a Mathematical Nature. Currently exploring game development, machine learning, and various programmatical oddities.
Chad Lumley on Twitter