Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Both gameobjects with the same script being destroyed when i only want one destroyed

Discussion in '2D' started by jpet1991, Oct 3, 2018.

  1. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    so i have a script attached to an enemy gameobject. When i have more than one enemies in the scene and when my destroy script is triggered, both end up being destroyed when i only need the one gone.

    I think i think this has to do with the objects not being instantiated, but i'm not sure. Can anyone point me in the right direction as to figure this out?

    Thank you!
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    are the enemies duplicated or instantiated from the prefab as clones ? And where is the destroy script
     
  3. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    right now they are just duplicated, i haven't written a spawn script just yet.

    the destroy script is attached to the enemy
     
  4. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    this is attached to the enemy object


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyObject : MonoBehaviour {
    6.  
    7.     [SerializeField] int timeToRemove = 3;
    8.     bool isFalling = false;
    9.  
    10.     //cached reference
    11.     DestroyObjectWhenFalling desObjFalling;
    12.    
    13.  
    14.  
    15.     //links references
    16.     private void Start()
    17.     {
    18.         desObjFalling = FindObjectOfType<DestroyObjectWhenFalling>();
    19.         desObjFalling.HasHitGround();
    20.     }
    21.  
    22.  
    23.  
    24.     //checks if the object has hit the ground to determine if it should start coroutine
    25.     void Update()
    26.     {                    
    27.         if (desObjFalling.HasHitGround() == true)
    28.             {
    29.             StartCoroutine(HitGround());
    30.                
    31.             }
    32.        
    33.     }
    34.  
    35.  
    36.  
    37.     //starts a timer to kill the gameobject after it collides with the ground
    38.     IEnumerator HitGround()                                            
    39.     {
    40.         Debug.Log("IM ABOUT TO DIE");
    41.  
    42.         yield return new WaitForSeconds(timeToRemove);
    43.         Destroy(gameObject);
    44.     }
    45.  
    46.     private void OnTriggerEnter2D()
    47.     {
    48.         isFalling = true;
    49.         Debug.Log("Im Falling");
    50.  
    51.     }
    52.  
    53.     public bool IsFallingTrue()
    54.     {
    55.  
    56.         return isFalling;
    57.     }
    58.  
    59.  
    60.  
    61. }
     
  5. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    and this is attached to my ground collider

    Code (CSharp):
    1.  
    2.  
    3. public class DestroyObjectWhenFalling : MonoBehaviour {
    4.     //cached reference
    5.     DestroyObject enemyDieHeight;
    6.     Collision2D collision;
    7.  
    8.     bool hasHitGroundTrue;
    9.  
    10.     //initialize link references
    11.     void Start()
    12.     {
    13.         enemyDieHeight = FindObjectOfType<DestroyObject>();
    14.         enemyDieHeight.IsFallingTrue();
    15.  
    16.     }
    17.  
    18.  
    19.  
    20.     //ground collider to detect true if enemy has fallen from sky
    21.     private void OnCollisionEnter2D(Collision2D collision)
    22.     {
    23.         if (enemyDieHeight.IsFallingTrue() == true) {
    24.             hasHitGroundTrue = true;
    25.         }
    26.     }
    27.  
    28.  
    29.     //returnable bool to destroy object script
    30.     public bool HasHitGround() {
    31.         return hasHitGroundTrue;
    32.     }
    33.  
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm betting these lines in the Start methods of both your scrips are the culprits:
    Code (CSharp):
    1. desObjFalling = FindObjectOfType<DestroyObjectWhenFalling>();
    Code (CSharp):
    1. enemyDieHeight = FindObjectOfType<DestroyObject>();
    All of the various
    GameObject.Find...
    methods search the entire scene for a reference, meaning it is not guaranteed to reference one specific object in particular.

    You'd be much better off just making your references public and supplying them from the inspector.
     
  8. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    i reworked the code as I think I was overthinking it so now it's much more concise, but it works the way i want it to. Thanks as always for your quick responses guys!
     
    Last edited: Oct 5, 2018