Unity的C#編程教程_41_array 數組挑戰1

  • 任務說明:

    • 設定一個數組,存儲 4 個名字,並列印最後一個名字
    • 設定一個數組,存儲 4 個等級,對應上面的 4 個名字,列印最後一個的等級
    • 設定一個數組,存儲 4 種武器,對應上面 4 個名字,列印最後一種武器
  • 附加任務:

    • 按下空格鍵的時候進行列印
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrayOverview : MonoBehaviour
{
		// 初始化 3 個數組
    public string[] names = new string[] { "Magician", "Monk", "Hero", "Demon" };
    public int[] level = new int[] { 20, 18, 16, 33 };
    public string[] weapons = new string[] { "axe", "hand", "sword", "spear" };


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) // 檢測按下空格
        {
            Debug.Log("The level of " + names[3] + " is " + level[3] + " and his weapon is " + weapons[3]); // 按照序號索引元素,進行列印
        }
    }
}

這裡也可以先初始化空列表,然後到 unity 中進行設定元素個數和賦值

Tags: