Alright this is probably going to take some time so I’ll get straight into it, I am working on a rogue-like dungeon crawler and I’ve been encountering some issues featuring the enemy type nicknamed the Mage, Every 2 seconds it Instantiates an orb that follows the player using the following script:
public Collider2D self;
public float moveSpeed;
public float attackDamage;
Transform Player;
bool temp;
void Start() {
if(PlayerState.health > 0f && GameObject.FindWithTag("Player").transform != null){
Player = GameObject.FindWithTag("Player").transform;
}
}
void Update(){
if(PlayerState.health > 0f && Player != null && GameObject.FindWithTag("Player") != null) {
transform.position = Vector2.MoveTowards(transform.position, Player.position, moveSpeed * Time.deltaTime);
}
if(PlayerState.health <= 0f) {
gameObject.SetActive(false);
Destroy(gameObject);
}
}
The line “Player = GameObject.FindWithTag(“Player”).transform;” throws a NullReferenceException when the player dies. In the script above you can witness my futile attempts to fix this, which have all returned unsuccessful. The player’s Death is handled with the next script:
void Reset () {
Debug.Log("PLAYER SHOULD BE DEAD REEEEEEEEEEE");
RoomSpawner.deleteAllRooms();
SceneManager.LoadScene("Prison");
}
IEnumerator DeathAnimation() {
Player.SetActive(false);
deathEffect.Play();
yield return new WaitForSeconds(1.5f);
Reset();
}
In theory, these are all the scripts affecting this issue, and it’s rather important since death is a pretty important part of a roguelike. What could be done to either make this script better or simply make it WORK again.