How to code in Unity with C# – 07 – array[ ]

Hi, This is seventh video on “How to code in unity with C#” series. I will explain you how array works in c# and how to use it in unity.

CODE:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TutorialSeven : MonoBehaviour
{
    public string[] weaponNames = { "Bow", "Sword", "Dagger", "Axe" };
    public Transform[] weaponContainer;
    // Start is called before the first frame update
    void Start()
    {
        //ShowWaponNames();
        ShowOnlyMeleeWeapons();
    }

    public void TestTablePos()
    {
        weaponNames = new string[3];
        weaponNames[0] = "Bow";
        weaponNames[1] = "Sword";
        weaponNames[2] = "Dagger";
    }

    public void ShowWaponNames()
    {
        foreach(string weaponName in weaponNames)
        {
            Debug.Log(weaponName);
        }
    }

    public void ChangeOnTablePos()
    {
        weaponNames[1] = "Hammer";
    }

    public void ShowOnlyMeleeWeapons()
    {
        foreach(Transform weapon in weaponContainer)
        {
            if (weapon.GetComponent<Weapon>().melee)
            {
                Debug.Log(weapon.GetComponent<Weapon>().name);
            }
        }
    }
    
    // Update is called once per frame
    void Update()
    {
        
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *