Unity — Setting up a game object, script, fixing class name mismatch. Day 1

Benjamin Calvin
3 min readSep 8, 2021

--

Objective: Understand how to create a basic game object, script, and fix a class name mismatch.

This will be a series of articles that will lead up to the animation below:

Starting with the aspect ratio. Ensuring that your ratio is 16:9 we ensure the screen size is compatible with majority of devices, be it PC or a mobile device. At the top of the game view you can select the aspect ratio.

Click the Aspect > then click 16:9 aspect.

This will put a checkmark next to 16:9 Aspect, save. Good to move forward.

Now that our view is setup correctly, let's create a cube object

Right-click on an empty spot in the list of game objects > 3d objects > cube. Name it player.

If we try to do anything, nothing happens. That’s because we don’t have anything telling the cube what to do. Let’s fix that.

In the project folder let’s create a script folder to hold our scripts in.

After creating our folder let's add a script, call it player. Ensure that the file is named the moment of creation. If you rename it later, the class within the C# script will retain its old name.

ut oh, looks like I didn’t follow my advice. Let’s rename the file to Player and attach the file to the Player object we created earlier. Attaching a script in Unity is as easy as click and drag the script onto the object.

Besides this class cannot be found error, there are some monobehavior scripts that can’t load as well. Let’s fix this by changing the class name and add a Debug.Log(“I work!”) to the Start() method.

After fixing the script, lets save, go back to Unity, attach the script to the object, and press play

As you can see MonoBehavior was properly inherited into the Player class. We no longer receive an error and our message of “I work!” printed to the console.

In the next article, we will modify the player object for movement utilizing input from the keyboard. Click the link below to continue with the follow on article.

https://medium.com/@benjamin.calvin/unity-creating-player-movement-2-2-97ec8c663014

--

--

Benjamin Calvin

Dedicated and motivated individual learning programming and sharing my discoveries with you.