Enemy not able to update score when destroyed.
I have a Null Reference exception on my enemy, it's a prefab and it's instantiated in a spawn object it's also derived from a list. I took the enemy out and added the characterStats object it was asking for, I saved it and then played game again error still persists. Any help would be appreciated.
Here is the code. using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class FootDoDamage : MonoBehaviour
{
GameManager gameManager;
CharacterStats characterStats;
PlayerController playerController;
public Animator anim;
public GameObject explosionPrefab;
public AudioSource footExplosionSound;
public EnemySight enemySight;
public GameObject punchAttackObj;
public GameObject kickAttackObj;
public int footHealth = 100;
public int footScore = 1;
void Start()
{
if (explosionPrefab ==null)
{
explosionPrefab = GetComponent<GameObject>();
}
if(footExplosionSound == null)
{
footExplosionSound = GetComponent<AudioSource>();
}
footExplosionSound = GetComponent<AudioSource>();
if (anim == null)
{
anim = GetComponent<Animator>();
}
if (playerController == null)
{
playerController = GetComponent<PlayerController>();
}
if (enemySight == null)
{
enemySight = GetComponent<EnemySight>();
}
if (gameManager == null)
{
gameManager = GetComponent<GameManager>();
}
if(characterStats == null)
{
characterStats = GetComponent<CharacterStats>();
}
}
// Update is called once per frame
void Update()
{
}
public void HidePunchAttack()
{
enemySight.anim.SetBool("attackcombo", false);
punchAttackObj.SetActive(false);
}
public void ShowPunchAttack()
{
enemySight.anim.SetBool("attackcombo", true);
punchAttackObj.SetActive(true);
}
public void HideKickAttack()
{
enemySight.anim.SetBool("attackcombo", false);
kickAttackObj.SetActive(false);
}
public void ShowKickAttack()
{
enemySight.anim.SetBool("attackcombo", true);
kickAttackObj.SetActive(true);
}
public void FootTakeDamage()
{
//if (this.gameObject != null)
if(GameObject.Find("FootSolder(Clone)"))
{
// Taking away health based on sword attack
footHealth -= 25; // take away 50 from attack
anim.SetBool("hit", true);
anim.SetBool("attackcombo", false);
anim.SetBool("walk", false);
gameObject.SetActive(true);
if (footHealth <= 0)
{
characterStats.AddScore();
anim.SetBool("hit", false);
anim.SetBool("defeated", true);
CancelInvoke("FootSolder");
StartCoroutine(HideFootSolder()); // Call the coroutine here and hide object
enemySight.navMeshAgent.updateRotation = false;
enemySight.navMeshAgent.angularSpeed = 0;
enemySight.navMeshAgent.isStopped = true;
//enemySight.navMeshAgent.enabled = false;
}
}
}
// Hide object in Coroutine instead of out right destroying it and causing errors.
IEnumerator HideFootSolder()
{
//There was a major Missing Component exception and it seemed to be fixed.
yield return new WaitForSeconds(1.5f);
try {
footExplosionSound.Play();
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
//Destroy(gameObject, 1.5f);
gameObject.SetActive(false);
//GetComponent<CapsuleCollider>().enabled = false;
// GetComponent<CharacterController>().enabled = false;
}
catch (MissingComponentException)
{
footExplosionSound = FindObjectOfType<AudioSource>();
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "lkatana" || other.tag == "rkatana" || other.tag == "leorfootattack")
{
FootTakeDamage();
// To Do Get the Blocked Colliders to turn off when players are defeated
print("Leo is hitting FootSolder.");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Utility;
public class Enemies : MonoBehaviour {
public Transform []spawnPosition;
public GameObject footPrefab; //footPrefab2, //footPrefab3;
public List<Transform> enemiesSpawned = new List<Transform>();
public FollowTarget followTarget;
public CameraFollow cameraFollow;
public GameObject blockColliders;
public bool wave1;
public bool wave2;
public bool wave3;
public int spawnRateMax = 6;
public int spawnRateMin = 1;
public float spawnTime = 3f;
public float countdown;
private float currentTime;
// Use this for initialization
void Awake ()
{
currentTime = countdown;
// InvokeRepeating("Wave1Attack", spawnTime, spawnTime);
if(cameraFollow == null)
{
cameraFollow = GetComponent<CameraFollow>();
}
if(blockColliders ==null)
{
blockColliders = GetComponent<GameObject>();
}
if(followTarget == null)
{
followTarget = GetComponent<FollowTarget>();
}
}
// Update is called once per frame
void Update ()
{
/*
countdown -= Time.deltaTime;
if(countdown <= 0)
{
wave1 = true;
Wave1Attack();
// camera stops following player when a wave is on
cameraFollow.enabled = false;
wave1 = false;
//print("time is up.");
}
else
{
wave1 = false;
CancelInvoke("Wave1Attack");
cameraFollow.enabled = true;
}
*/
/*
if(wave1 )
{
wave1 = true;
Wave1Attack();
}
if(!wave1)
{
wave1 = false;
CancelInvoke("Wave1Attack");
}
*/
}
public void BlockCollidersOn()
{
blockColliders.SetActive(true);
followTarget.enabled = true;
}
public void BlockCollidersOff()
{
blockColliders.SetActive(false);
followTarget.enabled = false;
}
public void SpawnEnemies()
{
int ranValue = Random.Range(spawnRateMin, spawnRateMax +1);
for (int i = 0; i < ranValue; i++)
{
int ranSpawn = Random.Range(0, spawnPosition.Length);
GameObject go = Instantiate(footPrefab, spawnPosition[ranValue].position, Quaternion.identity) as GameObject;
enemiesSpawned.Add(go.transform);
}
}
/*
public void Wave1Attack()
{
int spawnPointIndex = Random.Range(0, spawnPosition.Length);
Instantiate (footPrefab, spawnPosition[spawnPointIndex].position, spawnPosition[spawnPointIndex].rotation);
}
*/
}
Answer by Numonic · Dec 28, 2017 at 04:06 PM
Hopefully I can be of some help to anyone with this problem I was dealing with. I wound up in my awake method for FootDoDamageScript. I wound up reference the GameObject and the script together, that seemed to fix it and remove the NullReference I was getting.
Awake()
{
characterStats = GameObject.Find("characterStats").GetComponent();
}
Answer by ransomink · Dec 29, 2017 at 01:07 AM
In your first post, you don't give us any detail information about the null ref error. what line is it on, where in your code is it? no way for us to tell you what is null. It's waaay too much code to look through...
Hey thanks for the response I did in fact add the information via a the images I provided I just put up my code so I can show what was going on. Generally Unity Answers are a place to let your answers perish. The forums are a bit more robust in help. Thank fully I was able to resolve the issue but help did come via the Unity forums ins$$anonymous$$d of the help section.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 0 Answers
How do I fix a Null Reference Exception (object reference not set to an instance of an object)? 2 Answers
JS NullReferenceException: Object reference not set to an instance of an object 1 Answer
javascript NullReferenceException: Object reference not set to an instance of an object 0 Answers
NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer