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 Sirm · Aug 24, 2017 at 03:26 PM · runtime-errortargetting

Odd Targetting Error on Runtime

so the error i am having has had me wondering what to do about it for quite a few months, and since i have moved on to other parts of my project

this script is attached to a homing missile that detects and sorts all current enemies by distance from the player and then selects the closest one as its target

the error thrown rarely on runtime is this:

NullReferenceException: Object reference not set to an instance of an object targetting.m__0 (UnityEngine.Transform t1, UnityEngine.Transform t2) (at Assets/Scripts/Combat Scripts/targetting.cs:58) System.Array.qsort[Transform] (UnityEngine.Transform[] array, Int32 low0, Int32 high0, >System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1773) System.Array.Sort[Transform] (UnityEngine.Transform[] array, Int32 length, System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1700) Rethrow as InvalidOperationException: Comparison threw an exception. System.Array.Sort[Transform] (UnityEngine.Transform[] array, Int32 length, System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1703) System.Collections.Generic.List`1[UnityEngine.Transform].Sort (System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:579) targetting.SortTargetsByDistance () (at Assets/Scripts/Combat Scripts/targetting.cs:57) targetting.TargetEnemy () (at Assets/Scripts/Combat Scripts/targetting.cs:74) targetting.Update () (at Assets/Scripts/Combat Scripts/targetting.cs:95)

and this error is spammed for about ten seconds(which is when the object this script is attached to is set to dissapear if it has not collided with anything) basically cutting fps in half

the only thing i have been able to determine is when this error occurs which is when it detects all the enemies and begins sorting but in the time between detecting and sorting, one of the enemies is killed and this error is thrown

this is the script and i'm not sure what to do about it coming up with a null reference while sorting

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class targetting : MonoBehaviour {
     public Transform target;
     private Vector3 v_diff;
     private float atan2;
     public float moveSpeed = 5;
     public float trackSpeed;
     public List<Transform> targets;
     public Transform selectedTarget;
     private Transform myTransform;
     public bool fire;
     public bool canfire;
 
     // Use this for initialization
     void Start () {
         if(Combat.energy > 15)
         {
 
         targets = new List<Transform>();
         selectedTarget = null;
         trackSpeed = 5;
         myTransform = transform;
         AddAllEnemies();
         TargetEnemy();
         }
         else
         {
             gameObject.SetActive(false);
         }
         
     }
     
     public void AddAllEnemies()
     {
         GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");
         
         foreach(GameObject enemy in go)
             AddTarget(enemy.transform);
     }
     public void AddTarget(Transform enemy)
     {
         targets.Add(enemy);
     }
     private void SortTargetsByDistance()
     {
     
     targets.Sort(delegate(Transform t1, Transform t2){ 
                 return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); });
         
     }
     private void TargetEnemy()
     {
         if(selectedTarget == null && targets.Count == 1)
         {
             selectedTarget = targets[0];
         }
         if(selectedTarget == null && targets.Count > 1)
         {
             SortTargetsByDistance();
             selectedTarget = targets[0];
         }
         
     }
     // Update is called once per frame
     void Update () {
         if(selectedTarget != null)
         {
         fire = true;
         v_diff = (selectedTarget.position - transform.position);
         atan2 = Mathf.Atan2 (v_diff.y, v_diff.x);
         
         transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg), trackSpeed * Time.deltaTime);
         transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
         }
         if(selectedTarget == null)
         {
             targets.Clear();
             targets = new List<Transform>();
             AddAllEnemies();
             TargetEnemy();
         }
         if(targets.Count == 0)
         {
             transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
         }
         
     }
 }
 



i'm open to suggestions at this point

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

0 Replies

· Add your reply
  • Sort: 

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

112 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

Related Questions

GameObject instanciating only once: Runtime Error 1 Answer

Targetting Script Errors 0 Answers

Accessing Invalid Property 0 Answers

The 3D camera no longer exists 2 Answers

exe caused an Access Violation (0xc0000005) 0 Answers


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