I originally wrote this on my site, https://blog.crazy.technology, which I will be taking down in a few days.
Aims to provide the reader with a brief guide to coding with Unity.
- Editor – The Unity IDE
- Scene – A collection of objects and scripts that are loaded and active together (like an Activity in Android)
- GameObject – Any object/item seen in the hierarchy tab
- Prefab – A GameObject (could have child GameObjects attached) with customized components/scripts attached. Common uses:
- Instantiate a prefab via script (Instantiate(prefab, parentTransform))
- Click and drag more copies of the prefab onto the scene, with different parameters
- Component – Anything that is attached to a GameObject. Components give GameObjects their position, behavior, and other attributes. A script attached to a GameObject is also a component.
There are 6 main tabs in the editor most commonly used. They are:
- Displays currently active scenes in the scene tab. (Recommendation: Unload/Remove scenes not currently in use)
- Shows GameObjects that are in the scene, and their relative hierarchy (parent-child relationship)
- Shows all files in the Assets folder of the project
- Unity creates multiple folders for each project, but the programmer only needs to access the Assets folder
- Shows the components/scripts that have been added to the currently selected GameObject
- Each component/script has variables that can be adjusted. The adjustments can be performed here.
- Note: If a script specifies a default value, and the value is changed in the inspector, the changes will take priority. These values are stored in the corresponding .meta file.
- Shows warnings, errors, and other Debug.Log() or print() messages
- Shows GameObjects currently in the scene. They can be dragged and dropped, or their positions can be modified in the Inspector
- Shows the game view, via the currently active camera
Scripting in C#
Scripts are attached to GameObjects to give them certain attributes and behaviours.
They can also manipulate other GameObjects, by providing the script with a reference. This is most easily achieved by declaring a
public variable, and assigning it in the Inspector via click-&-drag from the Hierarchy tab. In the example below, an
InputField is assigned to the
Variables declared without an access modifier are implicitly
private, while those declared
internal are available throughout the scripts with the same namespace, but are not exposed for modification in the inspector. If no namespace is referenced, the script will be available throughout the project.
static are shared across all instances of the script. Only
public, non-static variables will be available in the inspector for modification.
Commonly Used in Scripting
gameObject: Reserved variable name, refers to the GameObject the script is attached to. Same with
gameObject.GetComponent<XXXX>(): Gets a component
(XXXX)attached to the GameObject.
GetComponent<XXXX>can be used with
camera. Can be chained, eg.
Import & Export – Unity Packages (.unitypackages)
Often, a project will have components that can be re-used in other projects, or an entire project is meant to be included in the final application.
In this case, a Unity package (.unitypackage) is a way to easily export and import a project.
To import a package, select Assets > Import Package > Custom Package.
If you updated a package imported previously, when you perform this step, Unity will automatically detect the files that need to be updated. In either case, you will be allowed to select the files to import.
To export a package, select Assets > Export Package. You will be allowed to select the files to export.
Application freezes when you press play (Editor)
Occasionally, an infinite loop in the Start() method will cause the application to freeze.
More commonly, an exception was thrown at startup. This may cause the application to freeze for some time (up to 5mins). If it does not appear to be “Not Responding” in the task manager, waiting for the application to resume will allow you to identify the exception being thrown via the Console.
Application crashes on start (mobile device)
The application works fine in the Editor, but crashes on start when deployed. This problem is tricky. You will need to connect your device to your PC, and check the Android log output, either via Android Studio or the Android Device Monitor.
Application freezes after loading successfully (mobile device)
The application freezes on/immediately after showing the first screen. This is most likely due to an unsuccessful network connection attempt on the main thread, which causes the application to freeze until connection is successful, or the connection attempt times out. Implementing network connections on a separate thread will resolve this issue.
Recommended Intro Tutorial
Support our Contributors. They get a little something for every new Like we get ❤️