Unity/수업 내용
[3D] MiniRPG - 플레이어 버튼 생성
이지훈26
2021. 10. 22. 16:50
UI로 버튼 오브젝트를 생성한다
Text 이름을 캐릭터 생성으로 바꿔준다
스크립트 생성
코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestCreateHeroMain : MonoBehaviour
{
public Button btnCreatHero;
public GameObject prefab;
// Start is called before the first frame update
void Start()
{
this.prefab = Resources.Load<GameObject>("Prefabs/ch_02_01"); //동기
Debug.LogFormat("prefab: {0}", this.prefab);
this.btnCreatHero.onClick.AddListener(() =>
{
Debug.Log("click");
Instantiate<GameObject>(this.prefab);
});
}
// Update is called once per frame
void Update()
{
}
}
버튼 오브젝트를 넣고
ch_02_01 프리팹을 넣어준다
실행 했을 때 오류 상황
Instantiate[T](T original) 오류 라면
코드에서 -> Instantiate<GameObject>(this.prefab); 부분에서 비어 있다고 하는 것 같은데..
*프리팹 먼저 확인 해보자
코드 확인