Unity Basic Notes for Interview

GameObject

Unity’s GameObject class represents anything which can exist in a Scene.

GameObjects are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the functionality.

To give a GameObject the properties it needs to become a light, or a tree, or a camera, you need to add components to it. Depending on what kind of object you want to create, you add different combinations of components to a GameObject.

MonoBehaviour

The MonoBehaviour class is the base class from which every Unity script derives, by default.

The MonoBehaviour class provides the framework which allows you to attach your script to a GameObject in the editor, as well as providing hooks into useful Events such as Start and Update.

The diagram below summarizes how Unity orders and repeats event functions over a script’s lifetime.

Monobehaviour Flowchart

I make a metaphor, the Unity Engine gives the developer an empty rail tracks. GameObjects are those trains that could run on those tracks. To build your own tracks, the MonoBehaviour base class gives you a frame of a train, which allows you easily fill it up to build your own trains.

Prefab

Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene. You can override settings on individual prefab instances if you want some instances of a Prefab to differ from others. You can also create variants of Prefabs which allow you to group a set of overrides together into a meaningful variation of a Prefab. You should also use Prefabs when you want to instantiate GameObjects at runtime that did not exist in your Scene at the start.

Some common examples of Prefab use include:

Unity Built-in Render Pipeline

Unity Built-in Render Pipeline

Possible Questions

Can threads be used to modify a Texture on runtime?

Can threads be used to move a GameObject on the scene?

Explain why deferred lighting optimizes scenes with a lot of lights and elements.

Explain why Time.deltaTime should be used to make things that depend on time operate correctly.

Can two GameObjects, each with only an SphereCollider, both set as trigger and raise OnTrigger events?

Which of the following examples will run faster?

  1. 1000 GameObjects, each with a MonoBehaviour implementing the Update call back.
  2. One GameObject with one MonoBehaviour with an Array of 1000 classes, each implementing a custom Update() callback.
    • Example 2 is faster. The Update callback is called using a C# Reflection, which is significantly slower than calling a function directly. In our example, 1000 GameObjects each with a MonoBehaviour means 1000 Reflection calls per frame. Creating one MonoBehaviour with one Update, and using this single callback to Update a given number of elements, is a lot faster, due to the direct access to the method.

Explain, in a few words, what roles the inspector, project and hierarchy panels in the Unity editor have. Which is responsible for referencing the content that will be included in the build process?

Explain the issue with the code below and provide an alternative implementation that would correct the problem.

using UnityEngine;
using System.Collections;

public class TEST : MonoBehaviour {
    void Start () {
        transform.position.x = 10;
    }
}
using UnityEngine;
using System.Collections;

public class TEST : MonoBehaviour {
   void Start () {
        transform.position = new Vector3(10, transform.position.y, transform.position.z);
    }
}

Reference

  1. Unity Documentation
  2. 12 Essential Unity or Unity3D Interview Questions