Posts

Showing posts from August, 2020

Unity Quick Tip - Setting private Fields through the inspector

Image
It's generally good practice to make fields private if we don't want them to be changed from other scripts, but doing this means they aren’t available to be set from the Inspector. In this Unity quick tip we're going to look at how we can have the best of both worlds and have private fields that can be set through the inspector. Let's get started by creating a new 3D project. We’ll add a new script to the Main Camera by selecting it in the Hierarchy, clicking the 'Add Component' button in the Inspector, and then searching for the script component. We'll call the new script PrivateField and double click on it in the Project panel to open it in Visual Studio. Let’s add the following private field to the script. private float someValue; If we save the script and switch back to Unity, we can see that the field is not available in the Inspector. If we switch back to the script, we can instruct Unity that we want the field to be available in the Inspect

Unity Quick Tip - Increment Snap and Grid Alignment

Image
In this Unity quick tip we'll look at how to position game objects more accurately by using Increment Snap and Grid Alignment Let's get started by creating a new 3D project in Unity Hub. We'll add a cube to the scene by clicking the plus button on the Hierarchy panel and selecting 3D Object->Cube. With the cube selected in the hierarchy, we'll select the move tool at the top left of the screen. Normally, when moving an object, it can be moved to any position. This can sometimes make it difficult to align exactly how you want. To assist with this, we can move the objects in increments. To do this we hold down Ctrl when dragging on the coloured arrows to move the cube in increments of 0.25. We can change the size of the increments to better fit the movement we want to make. To do this, we'll select Edit->Grid and Snap Settings from the main menu, and then change the Increment Snap Move value to 1. If we try holding Ctrl and moving the cube again it will now move

Unity Quick Tip - Using Calculations to set Values

Image
In this quick tip we're going to look at a really useful feature of Unity, that allows you to input mathematical equations when setting values through the interface. If you'd prefer to watch the video version of this tutorial please click  here . To demonstrate this, we’ll create a new 3D Project. We’ll then add a cube by clicking the plus button on the Hierarchy panel and selecting 3D Object->Cube. Let’s say we wanted to double the scale of this cube along the x axis. We could work it out in our head and just set the value to two.  However, what if we now wanted to make it a seventh of the size. That’s a bit trickier to work out in our head. Fortunately, Unity allows us to enter calculations in these fields, so we can type the following and it will work it out for us. It also supports much more complex equations. For example, we could type the following and it would calculate the value. This same technique can be used when setting any number values. For another example we’l