Unity Unity Scripting and C# 1 — Questions and Answers
Question 1: What is the base class for all Unity scripts that attach to GameObjects?
- Object
- Behaviour
- MonoBehaviour (Correct answer)
- ScriptableObject
Correct answer: MonoBehaviour
MonoBehaviour is the base class for Unity component scripts, providing lifecycle methods like Start(), Update(), and OnEnable().
Question 2: In Unity C#, what is a Coroutine used for?
- Running code in a separate thread
- Pausing execution and resuming it over multiple frames without blocking the main thread (Correct answer)
- Caching assets at runtime
- Handling network requests synchronously
Correct answer: Pausing execution and resuming it over multiple frames without blocking the main thread
Coroutines use IEnumerator and yield return statements to spread execution across multiple frames while remaining on the main thread.
Question 3: What does the [SerializeField] attribute do in Unity?
- Makes a field visible in the Inspector even if it is private (Correct answer)
- Makes a public field hidden in the Inspector
- Saves the field to a JSON file
- Marks a method as callable from the Inspector
Correct answer: Makes a field visible in the Inspector even if it is private
[SerializeField] exposes a private field in the Unity Inspector without changing its access modifier, maintaining encapsulation.
Question 4: Which Unity event function is called exactly once when a script's GameObject first becomes active?
- Update()
- Awake()
- Start() (Correct answer)
- OnEnable()
Correct answer: Start()
Start() is called once before the first frame Update() if the script component is enabled, making it ideal for initialization that depends on other objects being ready.
Question 5: What is the purpose of the 'static' keyword on a Unity C# field?
- It prevents the value from being serialized
- It makes the field shared across all instances of the class rather than per-instance (Correct answer)
- It makes the field read-only
- It marks the field for garbage collection
Correct answer: It makes the field shared across all instances of the class rather than per-instance
A static field belongs to the class itself rather than any individual instance, so all scripts share the same value regardless of how many exist.
Question 6: In Unity, what does Object.Destroy() do when passed a GameObject?
- Immediately removes the object from memory
- Schedules the GameObject for removal at the end of the current frame (Correct answer)
- Disables the GameObject
- Removes all components but keeps the GameObject
Correct answer: Schedules the GameObject for removal at the end of the current frame
Destroy() schedules the object for deletion at the end of the current frame, so it remains accessible during the rest of the current frame's execution.
What is the base class for all Unity scripts that attach to GameObjects?