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("피해를 받았습니다.");
}
}