x


AI Script Errror Crash After Destruction

i am working on a first person shooting game and have recently finished shooting and damage scripts which has been a succsess however everytime i destroy a enemy the game goes black abruptly followed by this error meesage: missing efrense Exception: the object type 'Transorm' has been destroyed but you are stilly trying to accssess it.

here is the codeing block that this is linked too found in the enemy AI Script.

alt text

Screenshot3.png (223.6 kB)
more ▼

asked May 08 '12 at 02:37 PM

ryand444 gravatar image

ryand444
18 2 8 11

seriously spell checker?

Looking at that it looks the script that is being called would have been attached to the enemy that was destroyed. This would give you the error that you are seeing. How are you calling the function "CanSeeTarget"?

May 08 '12 at 03:16 PM RoflHarris
(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

to silicon how would i got about implementing the script into my exsisting script?

more ▼

answered May 28 '12 at 06:19 PM

ryand444 gravatar image

ryand444
18 2 8 11

(comments are locked)
10|3000 characters needed characters left

I actually had the exact same problem when I was working on my targetting script. What I ended up doing is making a check to the enemy health to make sure that if the health of the enemy was equal to 0, that I set the gameobject.active = false. After setting the gameobject.active = false, any time I would destroy a gameobject I would go back and get a list of new enemies that are on the current screen. See example:

public void TargetAllEnemies()
{
    //Make an array of gameobjects all with the tag of enemy
    GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    targets.Clear();

    foreach(GameObject enemy in go)
    {
       targets.Add(enemy.transform);        
    }
}

private void TargetEnemy()
    {
       if(selectedTarget == null)
       {
         SortTargetsByDistance();
         selectedTarget = targets[0];
       }
       else
       {
         if(!selectedTarget.gameObject.active)
         {    
          Destroy(selectedTarget.gameObject,0);
          TargetAllEnemies();
          SortTargetsByDistance();
          selectedTarget = targets[0];
         }
[.....]

The problem really exists that if you store an array of gameobjects and you destroy one of them that is supposed to exist in the array, you are trying to reference an object that no longer exists. Therefore, once the object is destroyed, you need to alter the array by grabbing a new set of gameobjects.

more ▼

answered May 08 '12 at 08:44 PM

Silicon gravatar image

Silicon
2 1 1

i hate too ask a bit too much but how would i go about adding this to the Ai script im fairly new to using Unity

May 13 '12 at 08:10 PM ryand444
(comments are locked)
10|3000 characters needed characters left

Just check if target exists before you access any properties of target.

if (!target) return false;
more ▼

answered May 08 '12 at 03:34 PM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

This is good but it still allocates memory for the reference of the destroyed target. I tried to post an answer but apparently it's still being reviewed. If he wants to store gameobjects in an array for say a targetting system, he would have to re-evaluate the condition of the array when any object is destroyed.

May 08 '12 at 03:41 PM Silicon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x982

asked: May 08 '12 at 02:37 PM

Seen: 333 times

Last Updated: May 28 '12 at 06:19 PM