Search Unity

Update an array if object destroyed

Discussion in 'Scripting' started by cryptoshaman, Feb 27, 2013.

  1. cryptoshaman

    cryptoshaman

    Joined:
    Oct 10, 2012
    Posts:
    15
    I am working on a auto targeting system but i get an error when I destroy a target. I think what I need is something like a trigger that updated every frame but I am not shore how to do that. I tried a Physics.OverlapSphere but its a little to confusing for me. Please help me out i have been going at this for two days now.

    ERROR

    The object of type 'Transform' 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.


    Code (csharp):
    1. #pragma strict
    2.  
    3. import System.Linq;
    4.  
    5. public var enemyTransforms = new List.<Transform>();
    6. var targetTransform : Transform;
    7. var targetVolume: GameObject;
    8.  
    9. function OnTriggerEnter (other : Collider) {
    10.   if (other.CompareTag ("enemy")) {
    11.     for (var n=0; n < 10; ++n) {
    12.  
    13. enemyTransforms[n] = GameObject.Find("boonCreature"+(n+1)).transform;}
    14. }
    15. }
    16.  
    17. function  LateUpdate() {
    18.  
    19.     var position = targetTransform.position;
    20.     var nearest = enemyTransforms.OrderBy(function(t){ return (position - t.position).sqrMagnitude; }).First();
    21.     var latestNearest= nearest;
    22.         Debug.Log("this is nearest"+nearest);
    23.  
    24. transform.LookAt(nearest,targetVolume.transform.up);
    25.  
    26.  if(enemyTransforms[0] != null){
    27.     for (var n=0; n < 10; ++n) {
    28. enemyTransforms[n] = GameObject.Find("boonCreature"+(n+1)).transform;}
    29.    enemyTransforms = new list[myLength];
    30. }
    31. }
    32.  
    33.  
    34.  
    35.  
     

    Attached Files:

    Last edited: Feb 27, 2013
  2. atrakeur

    atrakeur

    Joined:
    Jun 14, 2011
    Posts:
    134
    Another way to do it is to expose and Add and a Remove method so that each object can register/unregister himself to the list.
    Then create a component on your objects and use Start() and OnDestroy() to make it register/unregister himself.
    This, way, no need to check each frame if an object was deleted, and you'll avoid calling a method on a destroyed object.
     
  3. cryptoshaman

    cryptoshaman

    Joined:
    Oct 10, 2012
    Posts:
    15
    ok yeah that sounds like it could work I am going to give it a try, thanks
     
  4. cryptoshaman

    cryptoshaman

    Joined:
    Oct 10, 2012
    Posts:
    15
    cool I got it to work now. I ended up making two scripts. one attaches to the other objects array and the other clears the array when something is destroys. also it calculates distances.

    add me to array Script
    Code (csharp):
    1. //Create a Var so you can refer to the script
    2. var objectsArrayScript : ObjectsArray;
    3.  
    4. function Update()
    5. {
    6.     //Find the script  
    7.     objectsArrayScript = GameObject.Find("Start").GetComponent(ObjectsArray);
    8.  
    9.     //Adds the transform of the object this script is attached to our array
    10.     objectsArrayScript.allObjects.Add(this.transform);
    11. }
    object array script
    Code (csharp):
    1. //Need this for the ArrayList function enter code here
    2. import System.Collections.Generic;
    3. import System.Linq;
    4.  
    5. //Create Your Array
    6. var allObjects = new List.<Transform>();
    7. var targetTransform : Transform;
    8.  
    9. function  LateUpdate() {
    10.  
    11.     var position = targetTransform.position;
    12.     var nearest = allObjects.OrderBy(function(t){ return (position - t.position).sqrMagnitude; }).First();
    13.     var latestNearest= nearest;
    14.         Debug.Log("this is nearest"+nearest);
    15.  
    16. transform.LookAt(nearest);
    17.  
    18.  
    19. if(nearest != null){
    20. allObjects.Clear();
    21. //Debug.Log("nearest is null");
    22. }
    23.  
    24. }
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Seems a little inefficient, having the list populate every frame, only to then clear immediatly, just for the sake of finding which is closest.
    I would maintain a persistant list of all objects, and only interact with it to sort and get closest when a new one is added or deleted.
     
  6. cryptoshaman

    cryptoshaman

    Joined:
    Oct 10, 2012
    Posts:
    15
    yea that sounds like the right thing to do next thanks for the advice.