Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trouble destroying GameObjects after Instantiating them

Discussion in 'Scripting' started by NaS_SAMSEI, Sep 20, 2019.

  1. NaS_SAMSEI

    NaS_SAMSEI

    Joined:
    May 22, 2019
    Posts:
    2
    Hi,

    What I am trying to do is to have a starting sphere that the user has to touch. Once this is done, the starting sphere disappears and 3 spheres appear. The user has to touch them to make theme disappear.

    After making the starting sphere disappear, the 3 spheres are instantiated, but when I touch them, they don't get destroyed.

    Here is my script :
    Code (csharp):
    1.  
    2. public class ExerciceTest : MonoBehaviour
    3. {
    4.     public GameObject startSphere;
    5.     public GameObject sphere;
    6.     public GameObject user;
    7.     private GameObject startSphereClone;
    8.     List<GameObject> sphereList;
    9.     private bool sphereSpawned;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         sphereSpawned = false;
    15.         startSphereClone = Instantiate(startSphere, new Vector3(0, 17, 8), Quaternion.identity);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (EcartV3(user.transform.position, startSphereClone.transform.position))
    22.         {
    23.             Destroy(startSphereClone);
    24.             SpawnSpheres(new Vector3(3, 3, 15), new Vector3(-2, 15, 15), new Vector3(6, 19, 18));
    25.             destroySpheresOnContact(sphereList);
    26.         }
    27.     }
    28.  
    29.     private bool EcartV3(Vector3 a, Vector3 b)
    30.     {
    31.         return Vector3.SqrMagnitude(a-b) < 0.3f;
    32.     }
    33.  
    34.     private bool SpawnSpheres(Vector3 sphereAPos, Vector3 sphereBPos, Vector3 sphereCPos)
    35.     {
    36.         if (!sphereSpawned)
    37.         {
    38.             GameObject sphereA = Instantiate(sphere, sphereAPos, Quaternion.identity);
    39.             GameObject sphereB = Instantiate(sphere, sphereBPos, Quaternion.identity);
    40.             GameObject sphereC = Instantiate(sphere, sphereCPos, Quaternion.identity);
    41.             sphereList.Add(sphereA);
    42.             sphereList.Add(sphereB);
    43.             sphereList.Add(sphereC);
    44.             sphereSpawned = true;
    45.         }
    46.         return sphereSpawned;
    47.     }
    48.  
    49.     private void destroySpheresOnContact(List<GameObject>spheres)
    50.     {
    51.         foreach (GameObject sphere in spheres)
    52.         {
    53.             if (EcartV3(user.transform.position, sphere.transform.position))
    54.             {
    55.                 Destroy(sphere);
    56.             }
    57.         }
    58.     }
    59. }
    60.  
    It would be very helpful if any of you guys had any idea of what is wrong with my script,

    Thank you
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You call destroySpheresOnContact only if the user is close to the first sphere, but how this can happen if you destroy first sphere? I bet there must be error in console complaining on that line. In the destroySpheresOnContact you should check sphere for null before checking distance because it might be already destroyed.
     
  3. NaS_SAMSEI

    NaS_SAMSEI

    Joined:
    May 22, 2019
    Posts:
    2
    I managed to make it work, thanks for your advice