using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MovingObject
{
public int playerDamage;
private Animator animator;
private Transform target;
private bool skipMove;
protected override void Start()
{
GameManager.instance.AddEnemyToList(this);
animator = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").transform;
base.Start();
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
if (skipMove)
{
skipMove = false;
return;
}
base.AttemptMove<T>(xDir, yDir);
skipMove = true;
}
public void MoveEnemy()
{
int xDir = 0;
int yDir = 0;
if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon)
yDir = target.position.y > transform.position.y ? 1 : -1;
else
xDir = target.position.x > transform.position.x ? 1 : -1;
AttemptMove<Player>(xDir, yDir);
}
protected override void OnCantMove<T>(T component)
{
Player hitPlayer = component as Player;
animator.SetTrigger("enemyAttack");
hitPlayer.LoseHealth(playerDamage);
}
}
Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at <685c48cf8f0b48abb797275c046dda6a>:0) Enemy.MoveEnemy () (at Assets/Scripts/Enemy.cs:40) GameManager+d__13.MoveNext () (at Assets/Scripts/GameManager.cs:69) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <685c48cf8f0b48abb797275c046dda6a>:0)
Hi @Sakura1707 Its likely the real culprit is in the GameManager, which I expect is probably destroying the target object (or perhaps the enemy object itself). Since the error is on line 40, the most likely culprit is target. You can validate if target is null by a simple Debug.Log("Target: " + target);
addition right before line 40.
hi @AnOrdinarySandwich
Thanks for your answer.
When I added Debug.Log("Target: " + target);
and lauched the game in the console was a lot of these messages:
Target: null
UnityEngine.Debug:Log (object)
Enemy:MoveEnemy () (at Assets/Scripts/Enemy.cs:39)
GameManager/<MoveEnemies>d__13:MoveNext () (at Assets/Scripts/GameManager.cs:69)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
It was like that until I took it to the next level and the same error about destroying object came up again
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public float turnDelay = 1f;
public static GameManager instance = null;
public BoardManager boardScript;
public int playerHealthPoints = 100;
[HideInInspector] public bool playersTurn = true;
private int level = 3;
private List<Enemy> enemies;
private bool enemiesMoving;
// Start is called before the first frame update
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
enemies = new List<Enemy>();
boardScript = GetComponent<BoardManager>();
InitGame();
}
void InitGame()
{
enemies.Clear();
boardScript.SetupScene(level);
}
public void GameOver()
{
enabled = false;
}
// Update is called once per frame
void Update()
{
if (playersTurn || enemiesMoving)
return;
StartCoroutine(MoveEnemies());
}
public void AddEnemyToList(Enemy script)
{
enemies.Add (script);
}
IEnumerator MoveEnemies()
{
enemiesMoving = true;
yield return new WaitForSeconds(turnDelay);
if (enemies.Count == 0)
{
yield return new WaitForSeconds(turnDelay);
}
for (int i = 0; i < enemies.Count; i++)
{
enemies*.MoveEnemy();*
yield return new WaitForSeconds(enemies*.moveTime);*
}
playersTurn = true;
enemiesMoving = false;
}
}
@AnOrdinarySandwich