정주찬
2022. 8. 12. 11:43
Switch문
If나 Else문 같은 조건문으로 하나의 변수와 비교하여 참인 경우만 해당라인 코드를 실행한다.
Switch 문법
switch (비교할 변수)
{
// 비교할 변수와 case 뒤의 변수가 같을 때 아래 코드를 실행
// break;를 만나면 switch문 탈출
// default는 다른 case들이 변수와 일치하지 않을 때 실행
case valueA:
메서드A();
break;
case valueB:
메서드B();
default:
메서드C();
break;
}
CollisionHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionHandler : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
switch (collision.gameObject.tag)
{
case "Friendly":
Debug.Log("This thing is firendly");
break;
case "Finish":
Debug.Log("Congrats, yo, you finished!");
break;
case "Fuel":
Debug.Log("You picked up fuel");
break;
default:
Debug.Log("Sorry, you blew up!");
break;
}
}
}