using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform player;
public GameSystem system;
// Start is called before the first frame update
void Awake()
{
player = GameObject.Find("player").transform;
system = FindObjectOfType<GameSystem>();
system.EnemyCount += 1;
}
// Update is called once per frame
void Update()
{
}
public void TurnEnd_Enemy()
{
if(this.player.position.x > this.transform.position.x)
{
this.transform.Translate(new Vector2(1, 0));
}
else if(this.player.position.x < this.transform.position.x)
{
this.transform.Translate(new Vector2(-1, 0));
}
if(this.player.position.y > this.transform.position.y)
{
this.transform.Translate(new Vector2(0, 1));
}
else if(this.player.position.y < this.transform.position.y)
{
this.transform.Translate(new Vector2(0, -1));
}
system.actEmeyCount += 1;
}
}
์ ๋ํฐ ํด์ ๊ฒ์์ ๋ง๋ค๊ณ ์๋ ์ค์ ๋๋ค.
์ ๋ํฐ๋ฅผ ์ด์ฉํด ํด์ด ๋๋ ๋๋ง๋ค ์ ์ด ํ๋ ์ด์ด๋ฅผ ํฅํด ํ์นธ์ฉ ์ ์งํ๋ ์คํฌ๋ฆฝํธ๋ฅผ ๋ง๋ค์์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ์ ์ค๋ธ์ ํธ๋ฅผ ํ๋ฆฌํฉ์ผ๋ก ๋ง๋ค์ด ์ธ์คํด์คํ ์ํค๋ TurnEnd ํจ์๊ฐ ์คํ๋ ๋ ์ ๋๋กย ์๋ํ์ง ์์ต๋๋ค.
๋๋ฌด์ง ์ด๋๊ฐ ๋ฌธ์ ์ธ์ง ๋ชจ๋ฅด๊ฒ ์ด์ ์ง๋ฌธ ๋๋ฆฝ๋๋ค...
1. ์ ๋ํฐ์ฝ์์ฐฝ์ ๋นจ๊ฐ์๊ฒฝ๊ณ ๋ก ๋๋ ํผ๋ฐ์ค๊ฐ ๋จ๋๊ฒฝ์ฐ -> player๋ systemํด๋์ค๋ฅผ ์ ๋๋ก ๋ชป๋ฐ์์ฌ ํ๋ฅ ์ดํผ 2.TurnEnd_Enemy() ํจ์๋ฅผ ์ด๋์ ๊ฐ ์ ๋๋ก ํธ์ถ์ ํด์ฃผ๋์ง 3. ์ ์ค๋ธ์ ํธ๊ฐ ํ๋ฆฌํฉ์ Enemyํด๋์ค๊ฐ ์ ๋๋ก ๋ถ์ด์๋์ง
ํ๋ฆฌํน์ผ๋ก ๋ง๋ค๊ธฐ์ ์ ์ ๋์ํ๋ค๋ ๋ป์? TurnEnd_Enemy๊ฐ ํธ์ถ๋๊ณ ์๋๊ฑด ํ์คํจ? Debug.Log ํจ์๋ฅผ ํตํด์ ์ด๋ ์ฝ๋๊ฐ ์ด๋ป๊ฒ ์คํ๋๋์ง ํ ์คํธํด๋ด ์ผ๋จ ํจ์ ์คํ์ด ๋๋์ง ๋ก๊ทธ ๋จ๊ฒจ๋ณด๊ณ , ํ๋ ์ด์ด๋ ์๋๋ฏธ ํฌ์ง์ ์ด ์ด๋ป๊ฒ ์ฐํ๋์ง ๋ณด๊ณ ๋ฑ๋ฑ - dc App
๊ทธ๋ฆฌ๊ณ "์ ๋๋ก ์๋ํ์ง ์๋๋ค" ๋ผ๋๊ฑด ํ์์ ์ค๋ช ํ ๋ ๋ณ๋ก ์ข์ ๋ฐฉ๋ฒ์ด ์๋. ํ์นธ์ฉ ์์ง์ฌ์ผ๋๋๋ฐ ์์ ์์์ง์ธ๋ค ๋ผ๋์ง, ์ด์ํ๊ณณ์ผ๋ก ์์ง์ธ๋ค ๋ฑ ์ ๋ณด๋ฅผ ๋ ๋ด์์ ์ง๋ฌธํด์ฃผ๋ฉด ๋ ์ข์ ๋ต๋ณ์ ๋ฐ์ ์ ์์๊ฑฐ์ - dc App
์ ๊ฐ์ฌํฉ๋๋ค! ํ๋ฆฌํฉ์ผ๋ก ๋ง๋ค๊ธฐ ์ ์ ์ ๋์ํ์ต๋๋ค. Debug.Log ํจ์๋ฅผ ์ฌ์ฉํด TurnEnd_Enemy ํจ์๊ฐ ํธ์ถ๋๋ ๊ฒ์ ํ์ธํ์ต๋๋ค. ํจ์๊ฐ ํธ์ถ๋๊ณ ์ฝ๋๊ฐ ์คํ๋์ง๋ง ํ์นธ์ฉ ์์ง์ฌ์ผํ๋๋ฐ ์์ ์์ง์ด์ง ์์ต๋๋ค. ์์ง์ด๋ ๋ถ๋ถ์์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค
ํ๋ ์ด์ด ์์น๋ ์ ์์น๋ ๋๊ฐ์๊ฑฐ ์๋? ใ ใ
์ค๋ฅ๋ก๊ทธ๋ฅผ๋ณด์ฌ์กฐ~
UnassignedReferenceException: The variable player of Enemy has not been assigned. You probably need to assign the player variable of the Enemy script in the inspector. ๋ผ๊ณ ๋น๋๋ค!
ํน์ Find์์ player ์ฐพ์๋ ์ด๋ฆ ๋์๋ฌธ์๋ ์ผ์นํจ?
๋ต ์ ๋ถ ์ผ์นํฉ๋๋ค
player ๋๋ฒ๊ทธ ์ฐ์ด๋ด์ค
player ๋ณ์๊ฐ ๋น์ด์๋์
์ผ๋จ Unassigned๋ผ๋๊ฑด, ๋ณ์์ ๊ฐ์ด ํ ๋น๋์ง ์์๋ค ๋ผ๋ ๋ป์ธ๋ฐ Findํจ์ ์คํ๋ ์งํ์ Debug.Log(player) ํ๋ฒ ์คํํด๋ณด์ ์ฌ๊ธฐ์ null์ด๋จ๋ฉด Findํจ์๊ฐ ์ ์๋์ ์ํ๊ฑฐ๊ณ , ์ฌ๊ธฐ์ null์ด ์๋๊ฒ ๋ฌ๋ค๋ฉด player ๋ณ์๋ฅผ Awake ์์ ์ดํ์ ์ ๋ํฐ ์๋ฆฌ์ผ๋ผ์ด์ ๊ฐ null๋ก ๋ฐ์๋ฒ๋ฆฐ๊ฑฐ์. Findํจ์๊ฐ ์ ์ ๋์์ํ๋ค๋ฉด, ๊ทธ ์ด์ ๋ฅผ ์ฐพ์๋ณด์๊ณ .. ๋ง์ฝ ๊ทธ๊ฒ์๋๊ฒฝ์ฐ ์ข ๋ณต์กํ๋ฐ.. public์ผ๋ก ์ ์ธ๋ ๋ณ์๋ inspector์์ ๋ณด์ด๋ ์๋ฆฌ์ผ๋ผ์ด์ฆ ํ๋๋ฅผ ๊ฐ๊ฒ๋จ. ํ๋ฆฌํน์ผ๋ก ๋ง๋ค๋ฉด ๊ทธ ์๋ฆฌ์ผ๋ผ์ด์ฆ ํ๋ ๊ฐ์ ์์ฑ์์ ํ ๋นํ๊ธฐ๋๋ฌธ์ ์์๋ฌธ์ ๊ฐ ์๊ธด๊ฑธ๋ก ์ถ์ ๋ - dc App
๊ทธ๋์ ํ์์ธ ๊ฒฝ์ฐ์ ์ด๋ป๊ฒ ํด๊ฒฐํ๋? public๋ณ์๊ฐ ์๋๋ผ private๋ณ์๋ก ๋ฐ๊พธ๊ฑฐ๋, ๋ง์ฝ ์ธ๋ถ์ ๊ทผ์ด ํ์ํด์ public์ ์ ์งํด์ผ๋๋ค๋ฉด ํ๋กํผํฐ๋ฅผ ์ฐ๋ฉด ๋จ. ์ ๋ํฐ๊ฐ public๋ณ์๋ ์๋ฆฌ์ผ๋ผ์ด์ฆ ํ๋๋ฅผ ๋ง๋ค์ด๋ฒ๋ฆฌ๊ธฐ๋๋ฌธ์.. - dc App
private ๋ณ์๋ก ๋ง๋๋๋ฒ์ ์ํ ๊ณ , ํ๋กํผํฐ๋ ๋ชจ๋ฅผ ์ ์์ ๊ฒ ๊ฐ์๋ฐ ํ๋กํผํฐ๋ก ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ public Transform player { get; set; } ์ด๋ ๊ฒ ๋ฐ๊พธ๋ฉด ๋จ - dc App