Search Unity

Destroying gameObject error

Discussion in 'Scripting' started by jimma, Jun 14, 2013.

  1. jimma

    jimma

    Joined:
    Apr 3, 2013
    Posts:
    28
    Hi,

    So i am trying to fire my turret based on enemies current position by rotating the turret to enemies position. After 3 fires the enemy object dies and I intend to rotate my turret back to its original direction ie the forward. I try to search the enemy with the FindWithTag but it seems after i kill the enemy object the turret is still searching for the enemy object because of which it does not rotate back to its original position after killing the enemy. My below code would make more sense of what I am trying to achieve.

    Code (csharp):
    1.  
    2. void Start () {
    3.     Enemy = GameObject.FindWithTag("enemy").GetComponent<Transform>();
    4.     TurretBase = GameObject.FindWithTag("turretbaseparts").GetComponent<Transform>();
    5.     EnemyAgain = GameObject.FindWithTag("enemy");      
    6.     }
    7.  
    8. void Update () {
    9.  
    10. transform.position = new Vector3(TurretBase.transform.position.x + CannonXoffset,CannonHeight,TurretBase.transform.position.z + CannonZoffset);
    11.        transform.rotation = Quaternion.Lerp(transform.rotation,desiredRotation,Time.deltaTime * turnSpeed);
    12.        
    13.         Debug.Log(EnemyAgain.GetComponent<EnemyScript>().Health);
    14.         if(EnemyAgain.GetComponent<EnemyScript>().Health > 0)
    15.         {
    16.            desiredRotation = Quaternion.LookRotation((Enemy.transform.position - transform.position));     
    17.         }
    18.         if(EnemyAgain.GetComponent<EnemyScript>().Health <= 0)
    19.             {
    20.          Destroy(EnemyAgain.gameObject);
    21.          desiredRotation = Quaternion.LookRotation(transform.forward);
    22.             }
    23.                 .....
    24.  
    The Enemy assigned in the start is used to find the Enemy objects transform which I use in the desiredRotation to find its position.

    The EnemyAgain is a GameObject which I use to access the Enemy's script and get its Health variable.

    If the health is greater than 0 then the turret would continue shooting the enemy. If the health is below 0 the Enemy GameObject is destroyed and the turret's desiredRotation should be in the forward direction basically its desiredRotation should stop looking out for the Enemy object. But this setup throws me an error saying that the turret is still searching for the destroyed gameObject which i declared in my Start function. I hope I have not confused anyone :p
     
  2. Saldog

    Saldog

    Joined:
    Sep 12, 2012
    Posts:
    28
    It would be better for you to setup an array and make a list of enemies that are nearby and sort the array by the distance to the turret(if thats how you want it to work) because you might not always have an enemy nearby for it to shoot at start.
     
  3. jimma

    jimma

    Joined:
    Apr 3, 2013
    Posts:
    28
    Okay I managed to make a list of enemies and shoot the enemies based on its distance to the turret.

    Now i rotate the turret based on the nearestEnemy gameobject position. Once their health reaches zero I destroy them. After all the enemies are destroyed, I try to rotate the turret to originalRotation ( I have placed a random cube in my scene which marks all turrets original rotation )

    The turret is able to mark all enemies, kill them. But after all enemies are killed and when I am trying to rotate it back to the original rotation, it throws an error as it is still searching for all the enemies in the scene.

    Is there any other way I can achieve this?

    Here is my updated code

    Code (csharp):
    1.  
    2. void Start () {
    3.     TurretBase = GameObject.FindWithTag("turretbaseparts").GetComponent<Transform>();
    4.     TurretCube = GameObject.FindWithTag("turretCube").GetComponent<Transform>();
    5.     }
    6. void Update () {
    7.        
    8.         enemies = GameObject.FindGameObjectsWithTag("enemy");
    9.         counter += Time.deltaTime; 
    10.         transform.position = new Vector3(TurretBase.transform.position.x + CannonXoffset,CannonHeight,TurretBase.transform.position.z + CannonZoffset);
    11.    
    12.         float distance = Mathf.Infinity;
    13.         Vector3 position = transform.position;
    14.         foreach (GameObject enemy in enemies) {
    15.             Vector3 diff = enemy.transform.position - position;
    16.             float curDistance = diff.sqrMagnitude;
    17.             if (curDistance < distance) {
    18.                 closest = enemy;
    19.                 distance = curDistance;
    20.             }
    21.         }
    22.         nearestEnemy = closest;
    23.  
    24.         desiredRotation = Quaternion.LookRotation((nearestEnemy.transform.position - transform.position) + new Vector3(0,aimError,0));
    25.         originalRotation = Quaternion.LookRotation(TurretCube.transform.position - transform.position);
    26.        
    27.         if(nearestEnemy.GetComponent<EnemyScript>().Health > 0)
    28.         {
    29.         transform.rotation = Quaternion.Lerp(transform.rotation,desiredRotation,Time.deltaTime * turnSpeed);
    30.         }
    31.        
    32.         if(nearestEnemy.GetComponent<EnemyScript>().Health <= 0)
    33.         {
    34.         Destroy(nearestEnemy.gameObject);
    35.         transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.deltaTime * turnSpeed);
    36.         }
    37.        
    38.         if((Vector3.Distance(transform.position, nearestEnemy.transform.position) < distanceToEnemy)  counter >= shootDelayAmount  nearestEnemy.GetComponent<EnemyScript>().Health > 0)
    39.         {
    40.           counter = 0.0f;
    41.           Instantiate(bulletPrefab,bulletSpawnPoint.transform.position,transform.rotation);
    42.           CannonZoffset = 0.35f;
    43.         }
    44.        
    45.         else{
    46.         CannonZoffset = 0.0f;
    47.         }
    48.        
    49.         aimError = Random.Range(-0.001f,0.001f);   
    50.     }
    51.  
     
    Last edited: Jun 14, 2013
  4. Saldog

    Saldog

    Joined:
    Sep 12, 2012
    Posts:
    28
    it could be as simple as having a boolean active when the array list is populated and false when it is empty and have it look at an object in front of it that is the original rotation for it. better way for you to do it would be to record in a Vector3 the inital rotation of the turret and have it do its business killing things and then order it to MoveTowards the original position you specified at the very start.
     
  5. jimma

    jimma

    Joined:
    Apr 3, 2013
    Posts:
    28
    Hey bro, thanks a ton for your inputs, I somehow managed to change the code as you suggested and now everything is working fine.

    As you mentioned, I added a new Vector3 variable desiredPosition to track the enemy position and this time around i used the enemy array length to perform checks instead of the enemy health variable. Below is my final code. Hope it does not create any further problems in future.

    If anyone is interested in trying out my turret script can do so :)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TurretControl : MonoBehaviour {
    6.     Transform TurretCube;
    7.     Transform TurretBase;
    8.     public Transform bulletSpawnPoint;
    9.     public GameObject bulletPrefab;
    10.     public float CannonHeight = 0.0f;
    11.     public float CannonXoffset = 0.0f;
    12.     public float CannonZoffset = 0.0f;
    13.     public float turnSpeed = 2.0f;
    14.     public float shootDelayAmount = 3.0f;
    15.     float counter = 0.0f;
    16.     public float aimError = 0.0f;
    17.     Quaternion desiredRotation;
    18.     float distanceToEnemy = 25.0f;
    19.     float RotVel;
    20.     GameObject[] enemies;
    21.     GameObject closest;
    22.     GameObject nearestEnemy;
    23.     Vector3 DesiredPosition;
    24.     // Use this for initialization
    25.     void Start () {
    26.     TurretBase = GameObject.FindWithTag("turretbaseparts").GetComponent<Transform>();
    27.     TurretCube = GameObject.FindWithTag("turretCube").GetComponent<Transform>();   
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.        
    33.         enemies = GameObject.FindGameObjectsWithTag("enemy");
    34.         counter += Time.deltaTime; 
    35.        
    36.         transform.position = new Vector3(TurretBase.transform.position.x + CannonXoffset,CannonHeight,TurretBase.transform.position.z + CannonZoffset);
    37.         transform.rotation = Quaternion.Lerp(transform.rotation,desiredRotation,Time.deltaTime * turnSpeed);
    38.        
    39.         float distance = Mathf.Infinity;
    40.         Vector3 position = transform.position;
    41.         foreach (GameObject enemy in enemies) {
    42.             Vector3 diff = enemy.transform.position - position;
    43.             float curDistance = diff.sqrMagnitude;
    44.             if (curDistance < distance) {
    45.                 closest = enemy;
    46.                 distance = curDistance;
    47.             }
    48.         }
    49.        
    50.         nearestEnemy = closest;
    51.          
    52.         if(enemies.Length != 0  (Vector3.Distance(transform.position, nearestEnemy.transform.position) < distanceToEnemy))
    53.         {
    54.         DesiredPosition = new Vector3(nearestEnemy.transform.position.x,nearestEnemy.transform.position.y,nearestEnemy.transform.position.z);
    55.         desiredRotation = Quaternion.LookRotation(DesiredPosition);
    56.         }
    57.        
    58.         else
    59.         {
    60.         DesiredPosition = new Vector3(TurretCube.transform.position.x,TurretCube.transform.position.y,TurretCube.transform.position.z);
    61.         desiredRotation = Quaternion.LookRotation(DesiredPosition);    
    62.         }
    63.        
    64.         if((Vector3.Distance(transform.position, nearestEnemy.transform.position) < distanceToEnemy)  counter >= shootDelayAmount  nearestEnemy.GetComponent<EnemyScript>().Health > 0  enemies.Length != 0)
    65.         {
    66.           counter = 0.0f;
    67.           Instantiate(bulletPrefab,bulletSpawnPoint.transform.position,transform.rotation);
    68.           CannonZoffset = 0.35f;
    69.         }
    70.        
    71.         else{
    72.         CannonZoffset = 0.0f;
    73.         }
    74.        
    75.         if(enemies.Length !=0  nearestEnemy.GetComponent<EnemyScript>().Health <= 0)
    76.         {
    77.         Destroy(nearestEnemy.gameObject);  
    78.         }
    79.         aimError = Random.Range(-0.001f,0.001f);   
    80.     }
    81. }
    82.  
    83.  
     
  6. Saldog

    Saldog

    Joined:
    Sep 12, 2012
    Posts:
    28
    No worries Jimma,

    I'm glad i helped :D

    ~Lyl