Unity Unity Scripting and C# 2 — Questions and Answers
Question 1: What is a ScriptableObject in Unity primarily used for?
- Creating UI elements
- Storing shared data assets that exist independently of scene GameObjects (Correct answer)
- Scripting NPC behavior trees
- Handling network synchronization
Correct answer: Storing shared data assets that exist independently of scene GameObjects
ScriptableObjects are data containers saved as assets, useful for sharing configuration data between scenes without duplicating it in each GameObject.
Question 2: Which method would you use to find a GameObject in the scene by its name at runtime?
- GetComponent()
- Resources.Load()
- GameObject.Find() (Correct answer)
- FindObjectOfType()
Correct answer: GameObject.Find()
GameObject.Find() searches the scene hierarchy by name, but it's expensive and should be cached in Start() rather than called every frame.
Question 3: In Unity C#, what does the 'ref' keyword do when used in a method parameter?
- Creates a copy of the value
- Passes the variable by reference so the method can modify the caller's variable (Correct answer)
- Marks the parameter as optional
- Prevents the variable from being garbage collected
Correct answer: Passes the variable by reference so the method can modify the caller's variable
The ref keyword passes a variable by reference, allowing the called method to read and write the original variable in the caller's scope.
Question 4: What is the Unity Event System and why is it useful in scripts?
- A logging framework for debug messages
- A publisher-subscriber pattern using UnityEvent that decouples event senders from receivers (Correct answer)
- A coroutine scheduler
- A physics callback system
Correct answer: A publisher-subscriber pattern using UnityEvent that decouples event senders from receivers
UnityEvent implements a publisher-subscriber pattern, allowing scripts to broadcast events without direct references to the listeners.
Question 5: What does the 'RequireComponent' attribute do in Unity?
- Automatically adds a required component when this script is added to a GameObject (Correct answer)
- Marks a component as required for build
- Forces a component to initialize first
- Locks the component from being removed in the Inspector
Correct answer: Automatically adds a required component when this script is added to a GameObject
[RequireComponent(typeof(Rigidbody))] ensures the specified component is automatically added (and cannot be removed) when your script is attached.
Question 6: In Unity, what is the difference between Awake() and Start()?
- Awake() runs after all Start() calls; Start() runs first
- Awake() runs when the object is loaded regardless of component enabled state; Start() runs only if the component is enabled (Correct answer)
- Awake() only runs in Play Mode; Start() runs in Edit Mode too
- There is no difference
Correct answer: Awake() runs when the object is loaded regardless of component enabled state; Start() runs only if the component is enabled
Awake() fires immediately when the object is instantiated even if the component is disabled, while Start() only fires when the component is enabled and before its first Update().
What is a ScriptableObject in Unity primarily used for?