c#/수업 내용

유니티

yeeendy 2023. 1. 26. 17:58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    public Monster target2;
    public GameObject target;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(this.target); //GameObject 클래스의 인스턴스
        this.Attack();
    }

    void Attack()
    {
        Debug.LogFormat("{0}을 공격 합니다.", this.target);
        
        //게임오브젝트에서 컴포넌트를 가져올 때
        var monster = this.target.GetComponent<Monster>();
        monster.Hit();

        //컴포넌트에서 부모 게임오브젝트를 가져올 때
        //GameObject go = monster.GetComponent<GameObject>();
        GameObject go = monster.gameObject;
        var mon2 = go.GetComponent<Monster>();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Monster : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void Hit()
    {
        Debug.Log("피해를 받았습니다.");
    }
}

'c# > 수업 내용' 카테고리의 다른 글

LCRSTree  (0) 2023.01.17
인벤토리 만들기  (0) 2023.01.16
변하는 데이터 만들기 연습  (0) 2023.01.15
변하지 않는 데이터 만들기 연습 2 (Mission, Item)  (0) 2023.01.13
230113  (0) 2023.01.13