Hi, This is sisth video on “How to code in unity with C#” series. I will explain you how for and foreach loops works in c# and how to use it in unity.
CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TutorialSix : MonoBehaviour
{
string[] weaponNames = { "Sword", "Axe", "Dagger", "Bow" };
// Start is called before the first frame update
void Start()
{
//TestForLoop();
TestForeaachLopp();
}
public void TestForLoop()
{
for (int i = 1; i <= 5; i++) //>;<;=;<=;>=
{
Debug.Log(i);
}
}
public void TestForeaachLopp()
{
foreach(string weaponName in weaponNames)
{
if(weaponName == "Bow")
{
Debug.Log("Do your action here");
}
}
}
}