Scripts Explained

Before we begin with making Nuvi move, let’s first explain the structure of a Unity script. There are many ways to make Nuvi move within Unity, and one way to do this is to add a Script component onto Nuvi. A Script component is a component that you can make on your own from scratch.

An empty Unity Script will look like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmptyScript : MonoBehaviour
{
   void Start()
   {
   }

   void Update()
   {
   }
}

Each new script will have the first 3 lines that start with using. These lines are needed for Unity to be able to use this newly created script, so we generally leave these lines of code alone. The next line that starts with public class 'script_name' is needed for Unity to access this specific script.

Make sure the name of the script matches the ‘script_name’! If you decide to change the name of the script outside the script, it does not update the line of code containing the script name, so make sure to go back in and update it, or else the game won’t run!

Each script has two pre-made methods.

void Start(): This method is called only once when the script is initially called. It is usually used to initialize variables we may need throughout the script.

void Update(): This method is called every frame and is usually used when we want to change a game behavior.

There exists another method we will be using to make Nuvi move called void FixedUpdate().

void FixedUpdate(): This method is called less frequently than Update(), but is best used when dealing with physics for smoother movement, such as when using Rigidbody.

There will be other methods from the Unity library we will be using later, but for now this is all you need to know!