Consider a ScriptableObject named WeaponData with a field damage. The following code loads the asset and prints the damage value.
WeaponData weapon = Resources.Load<WeaponData>("Sword");
Debug.Log(weapon.damage);Assuming the Sword asset has damage = 15, what will be printed?
WeaponData weapon = Resources.Load<WeaponData>("Sword");
Debug.Log(weapon.damage);Resources.Load returns the asset if it exists in the Resources folder.
The Resources.Load method loads the ScriptableObject asset named "Sword" from the Resources folder. Since the asset exists and has damage = 15, printing weapon.damage outputs 15.
Which of the following is the main advantage of using ScriptableObjects to store game data in Unity?
Think about how data can be reused and edited in the Unity Editor.
ScriptableObjects allow you to create data assets that can be shared across many game objects, avoiding duplication and making data management easier.
Given this ScriptableObject class:
[CreateAssetMenu(menuName = "EnemyData")]
public class EnemyData : ScriptableObject {
public int health;
}
// In some MonoBehaviour:
EnemyData enemy;
Debug.Log(enemy.health);What error will this code produce at runtime?
Consider what happens if you try to access a field on a null reference.
The variable enemy is declared but never assigned an instance. Accessing enemy.health causes a NullReferenceException at runtime.
Choose the correct code snippet that defines a ScriptableObject class named ItemData with a public string field itemName and a CreateAssetMenu attribute.
Remember ScriptableObjects must inherit from ScriptableObject class.
Option B correctly uses the attribute and inherits from ScriptableObject. Option B wrongly inherits from MonoBehaviour. Option B misses inheritance. Option B incorrectly uses struct which cannot inherit.
You create a ScriptableObject asset named LevelSettings in the Resources folder. In your game, you load it twice using:
var settings1 = Resources.Load<LevelSettings>("LevelSettings");
var settings2 = Resources.Load<LevelSettings>("LevelSettings");How many separate instances of LevelSettings exist in memory after these calls?
Think about how Unity caches assets loaded from Resources.
Resources.Load returns the same instance of the asset each time it is called with the same path, so both variables reference the same ScriptableObject instance.