Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
July 31, 2014
arrowPress Releases
July 31, 2014
PR Newswire
View All
View All     Submit Event





If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 
Particular Problems with Public Variables in Unity
by Robert Plante on 02/11/13 12:57:00 am

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

 

The Unity documentation it indicates that all public variables are made available in the inspector for direct editing. While this is handy, it is also dangerous. This article will cover a few things regarding public variables, and how to avoid them in Unity.

Public Variables

In a earlier post I made mention of public variables, specifically in a class-based architecture. One of the dangers of public variables is that anything can change their value. When testing Napkin Western (working title) on a tablet, some of the variables were corrupted from the values set in the Inspector. The values changed from 6 and 15 to 2.435e-43 and 2.643e-45 respectively.

Solution

What's the solution? Simple, set the access to all your variables, functions, enums, etc. to exactly what they should be. Make only variables public that absolutely need to be. Then take advantage of the [HideInInspector] to keep certain public variables hidden, and [SerializeField] to show hidden or private variables. This way things won't get accidentally modified, and you will have access to all the pieces you need while retaining the proper level of access.

 


Related Jobs

Raven Software / Activision
Raven Software / Activision — Madison, Wisconsin, United States
[07.31.14]

Senior UI Engineer
Disney Consumer Products
Disney Consumer Products — Glendale, California, United States
[07.30.14]

Contract Game Programmer
Zindagi Games
Zindagi Games — Camarillo, California, United States
[07.30.14]

Software Engineer
Telltale Games
Telltale Games — San Rafael, California, United States
[07.30.14]

Core Technology – Client Network Engineer






Comments



none
 
Comment: