I’ve been working on a 2D game and just finished the attacks and I’m trying to make it so that this works with multiple enemies on the field at the same time. I keep getting this error after I kill one and try to kill the other. Also when I try and hit the second one, it kills the first one. ERROR: MissingReferenceException: The object of type ‘Animator’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Animator.SetBool (System.String name, Boolean value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:277)
bruteDeath.bruteAnimDeath () (at Assets/Scripts/bruteDeath.cs:41)
bruteHealth.Update () (at Assets/Scripts/bruteHealth.cs:36)
My scripts:
LevelController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour {
//Adds a defineable delay to the attack
public float playerAtAnLength;
//Says whether or not the player can attack
private bool playerCanAttack;
//Use this to reference the playerController script
public playerController thePlayer;
//Use this to reference the bruteController script
public bruteController theBrute;
public bruteDeath theBruteDeath;
//Used to store the brutes last position.
public Vector3 bruteLastPosition;
public Vector3 bruteLastRotation;
//Adds a defineable delay to the animation
public float playerAtAnDelay;
//Adds a defineable delay to the actual attack
public float playerAcAtDelay;
//Tracks the players velocity
public float playerVelocity;
//Allows you to define the amount that the attack should be moved left
public double moveLeftDist;
//Player Actual Attack Remove Time is the time that it takes for the attack collider to be removed
public float playerAcAtReTi;
//Allows me to set the object which would be spawned when the player is attacking
public GameObject playerActualAttackCollider;
//Is true if the last move was right
public bool lastMoveRight;
//Allows the conversion of the players position to a double
public double playerTransformXDouble;
//Allows you to set to players attack x location when looking left
public Vector3 playerLocationLeft;
//Allows you to define the players x position moved to the left
public float playerAttackMovedLeft;
// Use this for initialization
void Start () {
//Defines what thePlayer is in the playerController script
thePlayer = FindObjectOfType<playerController>();
theBruteDeath = FindObjectOfType<bruteDeath>();
theBrute = FindObjectOfType<bruteController>();
//Starts it out so that the player can attack
playerCanAttack = true;
}
// Update is called once per frame
void Update () {
//
//
//All needed to flip the player attack when moving left
//
//
theBruteDeath = FindObjectOfType<bruteDeath>();
theBrute = FindObjectOfType<bruteController>();
//Sets the playerVelocity to the live playerVelocity
playerVelocity = thePlayer.playerVelocity;
//Transfers the lastMoveRight variable to this script
lastMoveRight = thePlayer.lastMoveRight;
//Converts the position to a double
playerTransformXDouble = thePlayer.transform.position.x;
//Converts the double to a float
playerAttackMovedLeft = (float)(playerTransformXDouble - moveLeftDist);
//Sets the attack to the left of the player by a certain amount
playerLocationLeft = new Vector3(playerAttackMovedLeft, thePlayer.transform.position.y, thePlayer.transform.position.z);
}
//
//
//
//THIS CODE HAPPENS WHEN THE PLAYER ATTACKS OR ATTEMPTS TO ATTACK:
//
//
//
//Is able to be called from the playerController when the player attacks
public void playerAttack()
{
//Will run if the player can attack
if(playerCanAttack == true)
{
//Calls the playerAttackCo() to start
StartCoroutine("playerAttackCo");
}
}
//The Co-routine to add the delay into the attack, so you cant spam attack.
public IEnumerator playerAttackCo()
{
//Makes the player not able to spam the attack button
playerCanAttack = false;
//Starts both co-routines below
StartCoroutine("playerAtAnCo");
//Will run if the player is moving right
if(playerVelocity >= 0.1)
{
StartCoroutine("playerAcAtCoRight");
}
//Will run if the player is still
if(playerVelocity == 0)
{
StartCoroutine("playerAcAtCoStill");
}
//Will run if the player is moving left
if(playerVelocity <= -0.1)
{
StartCoroutine("playerAcAtCoLeft");
}
//Sets the canMove variable in the playerController to false
thePlayer.GetComponent<playerController>().canMove = false;
//Adds the actual delay for the number of seconds that playerAttackDelay is defined to.
yield return new WaitForSeconds(playerAtAnLength);
//Sets the canMove variable in the playerController to true
thePlayer.GetComponent<playerController>().canMove = true;
//When the delay is over, the player can attack again.
playerCanAttack = true;
}
//This Co-routine adds enough delay so that the actual animation is played only once
public IEnumerator playerAtAnCo()
{
//Sets the isAttacking variable in the playerController to true
thePlayer.GetComponent<playerController>().isAttacking = 1;
//Adds enough delay so that the animation is displayed
yield return new WaitForSeconds(playerAtAnDelay);
//Sets the isAttacking variable in the playerController to false
thePlayer.GetComponent<playerController>().isAttacking = 0;
}
//This Co-routine adds the actual damage part into the attack
public IEnumerator playerAcAtCoRight()
{
//Sets the delay for the actual damage to be given
yield return new WaitForSeconds(playerAcAtDelay);
//Spawns the attack radius
Instantiate(playerActualAttackCollider, thePlayer.transform.position, thePlayer.transform.rotation);
}
public IEnumerator playerAcAtCoLeft()
{
//Sets the delay for the actual damage to be given
yield return new WaitForSeconds(playerAcAtDelay);
//Still need to flip instantiate transform position
Instantiate(playerActualAttackCollider, playerLocationLeft, thePlayer.transform.rotation);
}
public IEnumerator playerAcAtCoStill()
{
//Sets the delay for the actual damage to be given
yield return new WaitForSeconds(playerAcAtDelay);
//Will run if the last move was right
if(lastMoveRight == true)
{
//Will run and place the attack radius on the right if the player is facing right
Instantiate(playerActualAttackCollider, thePlayer.transform.position, thePlayer.transform.rotation);
}
//Will run if the last move is left
if (lastMoveRight == false)
{
//Will run and place the attack radius on the left if is the player is facing left
Instantiate(playerActualAttackCollider, playerLocationLeft, thePlayer.transform.rotation);
}
}
//
//
//
//Will run when brute dies
//
//
//
public void bruteActualDeath(){
bruteLastPosition = new Vector3(theBrute.transform.position.x, theBrute.transform.position.y, theBrute.transform.position.z );
theBruteDeath.addDeadBrute();
}
}
Brute Death:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bruteDeath : MonoBehaviour
{
private bruteHealth theBrute;
public Sprite deadBrute;
private Animator myAnim;
public float timeToEnd;
public LevelManager theLevelManager;
public Sprite bruteSprite;
public Vector3 brutePosition;
public Vector3 bruteRotation;
public GameObject deadBody;
// Use this for initialization
void Start()
{
theLevelManager = FindObjectOfType<LevelManager>();
theBrute = FindObjectOfType<bruteHealth>();
myAnim = GetComponent<Animator>();
bruteSprite = GetComponent<Sprite>();
}
// Update is called once per frame
void Update() {
}
public void bruteAnimDeath()
{
myAnim.SetBool("bruteIsDead", true);
}
public void bruteDeathAnimDone()
{
myAnim.SetBool("bruteDeathAnimDone", true);
theLevelManager.bruteActualDeath();
}
public void addDeadBrute()
{
brutePosition = theLevelManager.bruteLastPosition;
Destroy(gameObject);
Instantiate(deadBody, brutePosition, theBrute.transform.rotation);
}
}
Brute Health:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bruteHealth : MonoBehaviour
{
//The health of the brute
public float bruteHealthPoints;
//References the playerController script
private playerController thePlayer;
private bruteDeath theBruteDeath;
private float playerVelocity;
private float playerAt1Dam;
// Use this for initialization
void Start()
{
theBruteDeath = FindObjectOfType<bruteDeath>();
//Actually finds the object with the playerController script
thePlayer = FindObjectOfType<playerController>();
//Sets the player Attack 1 Damage in this script to the defined amount in the playerController
playerAt1Dam = thePlayer.playerAttack1Dam;
}
// Update is called once per frame
void Update()
{
//Checks to see if the brute has run out of health
if(bruteHealthPoints <= 0)
{
//If the brute has no health, then they are removed from the world. Eventually, add an Instantiate with particales
theBruteDeath.bruteAnimDeath();
}
}
//Will run if the player attack 1 happens
public void thePlayerAttack1BruteDamage()
{
//Takes the player attack 1 damage amount away from the brutes health.
bruteHealthPoints = bruteHealthPoints - playerAt1Dam;
}
}
I also have a playerController script and bruteController script if you need them. I’ve been working on this for a few hours and am getting frustrated. Please help!!!