- Create
ItemType.cs
- Make
ItemType
derive fromScriptableObject
- Add a
string
field called "DisplayName"- We could add more meta information (e.g., stats, description, icon, etc.), but we don’t need to at the moment
- Each instance of
ItemType
will represent a type of item and will be referenced in our code instead ofstring
s - But how do we create item types?
- Add the
CreateAssetMenu
attribute- Like components that derive from MonoBehaviour, scriptable objects don't support constructors and can't be instantiated with
new
- You can use
ScriptableObject.CreateInstance
- But the
CreateAssetMenu
allows you to create your scriptable object assets right from the Unity editor, which is what we want
- Like components that derive from MonoBehaviour, scriptable objects don't support constructors and can't be instantiated with
- Change the
string
array inInventory
to anItemType
array - Change the
string
field inInteractionHandler
toItemType
- Switch to Unity
- Create a folder called “Content”
- Create a new instance of
ItemType
for each item (Sword, Potion, Scroll) - Update the scene and verify the behavior
Last active
September 30, 2021 20:18
-
-
Save charlieamat/cbd98c96602b0b805c9955cbea5ad9f1 to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2 — ScriptableObjects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Inventory : MonoBehaviour | |
{ | |
[SerializeField] private ItemType[] items; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CreateAssetMenu(menuName = "Game/Item Type")] | |
public class ItemType : ScriptableObject | |
{ | |
public string DisplayName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment