Skip to content

My Top 7 Tips for Unity Beginners

There’s a lot to learn when you’re just getting started with Unity, but here are a few quick tips that will save you a lot of face palming later on.

Always save script changes before running.

By default, Unity does not save your scripts before running. If you don’t save, the code you’re trying to debug and the code displayed in your IDE may be two different things.

Create prefabs to persist play mode changes.

Changes made to your scene while your game is playing are not persisted. This makes it really convenient to experiment with a range of value, positions, cameras, etc. without worrying about messing up your game, but it can be a pain to remember and restore all those values after exiting play mode.

One solution is to create a temporary prefab. The prefab objects are saved. If you’ve finally perfected some complex element, create a new prefab, drag the whole object you want to persist onto the empty prefab, and exit play mode. The prefab will stick around. Then you can copy/paste the values, copy/paste a specific component, or even completely replace your old object with the new prefab. When you’re done, delete the prefab and disconnect the new object from the prefab instance (GameObject -> Break Prefab Instance) to avoid cluttering up your project.

Bonus tip: To avoid forgetting you’re in play mode, set a play mode tint to the editor. Go to Preferences -> Colors -> General -> Playmode tint. All licenses now have this option.

Set public variables within the editor or hide them.

The editor values of any instance of a script will override the default public values set inside the script. For instance, if you have “public int playerSpeed = 5;” at the top of your script and set the playerSpeed to 10 in the editor, then your player will have a speed of 10. Changing the code to “public int playerSpeed = 15;” won’t change the playerSpeed. You’ll need to change the playerSpeed in the editor.

To prevent this behavior, you can make the variable private or add [HideInInspector][System.NonSerialized]. System.NonSerialized will keep the values in your script and HideInInspector will hide the variable from the editor, so it isn’t confusing where to set it.

Add rigid bodies to enable collisions.

You need a rigid body in one of the colliding objects (and colliders on both) for physics to work. Usually the rigidbody is put on the moving object (like the player). If you notice a collision not taking places, make sure both objects have colliders, neither collider is a trigger, and one of the objects has a rigidbody.

Set changing OnClick listeners in a script.

I just found out about this one recently. OnClick listeners set in the editor are persistent. This means that even if you call OnClick.RemoveAllListeners(); the OnClick listeners set in editor will stick around.

For example, I had an add button with a OnClick listener set in the editor. I wanted to keep the button at the end of a list of buttons of the same size, so I used the add button as a prefab when creating new buttons. Despite calling RemoveAllListeners after instantiating new buttons, the buttons kept calling the same function I set for the add button in the editor.

Add [SerializeField] to public enums.

This allows them to be selected by name in the editor instead of simply by value.

Add new enums to the end of your enum list.

While [SerializeField] shows the names of the enums, they’re still ints behind the scenes. Adding a new enum in a position other than last will cause the other enums to shift values.

For example, I have the letter enum: [SerializeField]public enum Letter { A, D, F, Size }. I already use the enum for various public variables selected in the editor. Adding B after A would change all editor references from D to B and from F to D. Behind the scenes, the variables remain the ints 1 and 2. The values have not changed, but the names have.

Bonus tip: Add SIZE as the last value of all your enums. It’s a useful value for iterating through the enums and as a temporary or debug variable for uninitialized enums.

Summary

Unity is an awesome tool, but as with any tool it takes time to learn. Hopefully, you can use these tips to skip my frustrations and get to making awesome games!

Do you know of other simple Unity tips or mistakes to avoid? Just want to vent a frustrating story? Add them in the comments!

Published inDevelopment Tips

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *