Unity lets you make games of any kind for any platform. It’s revolutionary but its flexibility can lead to a system which is hard to understand. Here’s what I learnt making my game as a handy reference.
Which building blocks do I use and when?
|What||The entities that interact with each other.||Data models editable via Unity UI.||
||Basic C# class.|
|When||If it can be seen in the game world.|| To define the settings of || To control || When behaviour not directly related to a |
|Where||Lives in the Hierarchy.||Lives in the ProjectView. Initialized by Unity.|| Attached to ||Lives in the ProjectView. Called programatically by other scripts.|
|Example|| || || || |
|Use Cases|| || || || |
In Unity most things are
GameObjects which scripts are attached to. In your scripts you can access most things with these 3 snippets:
- Get the instance of the script/class we’re in with
- Get the GameObject the script is attached to with
- Get the GameObject’s components with
The longer version
this.gameObject.GetComponent<ComponentName>()will also work.
If you’re searching for GameObjects in the Scene Hierarchy:
- Find a GameObject by name:
- Find a GameObject of a certain type:
- Find a GameObject with a certain tag:
Using breakpoints via Visual Studio is the best way to debug complex data flows, but sometimes you just want good old fashioned debugging.
List components and all their variables
using System.Reflection; at top of file then paste:
You will need to keep running the main scene and navigating to it in Unity then running it is tedious. Create a keyboard shortcut instead with:
<scene-name> with the name of your scene. Now
M key on your keyboard will run the main scene from anywhere.