Skip to content

Instantly share code, notes, and snippets.

@charlieamat
Last active September 13, 2021 19:58
Show Gist options
  • Save charlieamat/287e3c5a31c6a20784584f023e0e5cdd to your computer and use it in GitHub Desktop.
Save charlieamat/287e3c5a31c6a20784584f023e0e5cdd to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2 — Your First Script
  1. In Unity, Right-click the _Project/Scripts folder, expand the Create menu, and click C# Script
  2. Name the new file "Inventory" and open it in Visual Studio
  3. Add a public String array called "Items"
  4. Initialize the Items array in the Start method
    • e.g., "Sword", "Potion", "Scroll"
  5. Use a for-loop to iterate over the Items array
    • Log each item to the console
  6. Back in Unity, run the scene
    • Point out that nothing happens because the script hasn't been instantiated
  7. Add the Inventory component to the Player GameObject in the Scene
    • Show the Add Component menu
  8. Run the scene again and show the contents of the console window
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