Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Numonic · Dec 27, 2017 at 06:15 PM · nullreferenceexceptioninstanceenemies

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);
     }
     */
    }
 
 
 
 

tmnt.jpg (198.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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();
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Numonic · Dec 29, 2017 at 04:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges