프로그래밍 농장

인스턴스 [ Unity ] 본문

Unity

인스턴스 [ Unity ]

Tennessee201 2022. 7. 7.
728x90

- Instance (인스턴스 )

: 어떠한 대상을 실존하는 무언가로 찍어낸다 (=미리 만들어놓은 GameObject를 게임 도중 필요한 만큼 찍어냄)

ex) 총알이나 몬스터 등 . .

public class Spwan : MonoBehaviour
{
    public Transform spawnPosition;

    public GameObject target;

    // Start is called before the first frame update
    void Start()
    {
        //위치 & 회전도 아래와 같이 명시적으로 부여가능
        GameObject instance = Instantiate(target,
            spawnPosition.position,spawnPosition.rotation);

        instance.GetComponent<Rigidbody>().AddForce(0, 1000, 0);
        Debug.Log(instance.name);
    }

}

위와 같이 GameObject 타입의 instance를 Instantiate()를 통하여 생성하고, 이때 코드와 같이 게임오브젝트의 위치와 회전도 등을 부여할 수 있다. 

이후 생성해 놓은 instance의 Rigidbody에 y축 1000 force를 주면, Scene 시작과동시에 위로 force1000 만큼 튕겨올라갔다가 떨어진다. 


 

728x90