C#
[C#] 콘솔과 유니티에서 숫자 입력값 받기
테크앤아트
2023. 8. 2. 16:46
728x90
반응형
콘솔 실행창에서 Console.ReadLine(); 으로 입력값을 받을 때 string 형태를 받는 것이 일반적이므로 int a = Console.ReadLine(); 같이 쓰면 오류가 난다. 따라서 다음과 같은 방법으로 해결할 수 있다.
- TryParse
float a, b;
float.TryParse(Console.ReadLine(), out a);
float.TryParse(Console.ReadLine(), out b);
- Convert
int a;
String aValue = Console.ReadLine();
a = Convert.ToInt32(aValue);
유니티에서는 다음과 같이 값을 입력받을 수 있다.
public int aValue;
public int bValue;
void Start()
{
int a, b, c;
a = aValue;
b = bValue;
...
}
728x90
반응형