카테고리 없음

2.4.1 변수 사용하기

새벽감성개발자 2023. 9. 6. 17:00
반응형

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;                  // 유니티가 동작하는데 필요한 기능 제공


public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        float height1 = 184.5f;
        float height2;
        height2 = height1;
        Debug.Log(height2);  // height2 = height1 = 184.5f;

        // 결과값 출력 : 184.5
    }

    // Update is called once per frame
    void Update()
    {
        // 현재 캐릭터를 조금씩 오른쪽으로 옮기는 처리
        // 한 프레임마다 실행

    }
}

2.4.1 변수 사용하기

  • 스크립트에서는 변수를 사용해 숫자나 문자열을 다룬다.
  • 어떤 종류의 데이터를 넣을지, 데이터를 감싸는 이름을 무엇으로 할지 정한다.
  • 변수명은 중복이 불가능하다.

데이터형

using System.Collections;
using System.Collections.Generic;
using UnityEngine;                  // 유니티가 동작하는데 필요한 기능 제공


public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int age;
        age = 30;
        Debug.Log(age); // 나이를 출력
    }

    // Update is called once per frame
    void Update()
    {
        // 현재 캐릭터를 조금씩 오른쪽으로 옮기는 처리
        // 한 프레임마다 실행

    }
}

 

변수 초기화와 대입

using System.Collections;
using System.Collections.Generic;
using UnityEngine;                  // 유니티가 동작하는데 필요한 기능 제공


public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        float height1 = 184.5f;
        float height2;
        height2 = height1;
        Debug.Log(height2);  // height2 = height1 = 184.5f;

        // 결과값 출력 : 184.5
    }
}
변수 초기화 방법
데이터형 변수명 = 대입할 값;

변수에 변수를 대입하는 방법
변수명 = 대입할 변수명;

 

변수에 문자열 대입

using System.Collections;
using System.Collections.Generic;
using UnityEngine;                  // 유니티가 동작하는데 필요한 기능 제공


public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string name;
        name = "Dawn";
        Debug.Log(name);

        // 결과값 : Dawn
    }
}
String 형에 숫자를 대입하는 경우
숫자도 문자로 취급하기 때문에 입력은 되지만, string형과 int형과의 계산은 되지 않는다.
큰 따옴표로 묶어진 데이터는 문자열

 

반응형