***변수(Variable)***의 종류인데,

**"어디에서 선언되었는가?"**에 따라 사용 범위(Scope)와 유지 시간(Lifetime)이 달라져!

image.png

<aside> <img src="/icons/list-indent_blue.svg" alt="/icons/list-indent_blue.svg" width="40px" />

목차

</aside>

🏠 1. 멤버 변수 (Member Variable)

🔹 클래스 안에 선언된 모든 변수


class Player
{
    public int health = 100;  // 멤버 변수 (인스턴스 변수)
    public static int playerCount = 0; // 멤버 변수 (클래스 변수)
}

health → 멤버 변수 (각 객체마다 따로 존재 = 인스턴스 변수)

playerCount → 멤버 변수 (static이므로 모든 객체가 공유 = 클래스 변수)


🏛️ 2. 클래스 변수 (Class Variable)

🔹 static이 붙어서 모든 객체가 공유하는 변수

public class Player
{
    public static int PlayerCount = 0;  // 클래스 변수

    public Player()
    {
        PlayerCount++;  // 새로운 플레이어가 생성될 때마다 증가
    }
}
//Driver.Start()
Player player1 = new Player();
Debug.Log(Player.playerCount); // 1 (모든 객체가 같은 값을 공유)

Player player2 = new Player();
Debug.Log(Player.playerCount); // 2 (모든 객체가 같은 값을 공유)