- In Unity, Right-click the _Project/Scripts folder, expand the Create menu, and click C# Script
- Name the new file "Inventory" and open it in Visual Studio
- Add a public String array called "Items"
- Initialize the Items array in the Start method
- e.g., "Sword", "Potion", "Scroll"
- Use a for-loop to iterate over the Items array
- Log each item to the console
- Back in Unity, run the scene
- Point out that nothing happens because the script hasn't been instantiated
- Add the Inventory component to the Player GameObject in the Scene
- Show the Add Component menu
- Run the scene again and show the contents of the console window
Last active
September 13, 2021 19:58
-
-
Save charlieamat/287e3c5a31c6a20784584f023e0e5cdd to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2 — Your First Script
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
using UnityEngine; | |
public class Inventory : MonoBehaviour | |
{ | |
public string[] Items; | |
private void Start() | |
{ | |
Items = new [] { "Sword", "Potion", "Scroll" }; | |
for (var i = 0; i < Items.Length; i++) | |
{ | |
Debug.Log(Items[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment