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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need Objects to destroy

Discussion in 'Scripting' started by Greywolf6, Oct 22, 2021.

  1. Greywolf6

    Greywolf6

    Joined:
    Oct 20, 2020
    Posts:
    10
    I am using this code to randomly spawn objects to 6 spawn points. That all works, but the objects do not despawn when I pass/exit the collider. The ground tiles do, the collectables do but not these. I tried so many different things I broke my game and spent the last hour getting it back to where I was. Please help.




    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ObstacleSpawner : MonoBehaviour
    7. {
    8.     GroundSpawner groundSpawner;
    9.     public Transform[] spawnPoints;
    10.     public GameObject[] obstacles;
    11.  
    12.     int randomSpawnPoint, randomObstacle;
    13.     public static bool spawnAllowed;
    14.  
    15.     public void Start()
    16.     {
    17.        
    18.         spawnAllowed = true;
    19.         InvokeRepeating("SpawnAObstacle", 0f, 0f);
    20.     }
    21.  
    22.  
    23.     void SpawnAObstacle()
    24.     {
    25.         if (spawnAllowed)
    26.         {
    27.         randomSpawnPoint = Random.Range (0, spawnPoints.Length);
    28.         randomObstacle = Random.Range (0, obstacles.Length);
    29.         Instantiate (obstacles [randomObstacle], spawnPoints [randomSpawnPoint].position, Quaternion.identity);
    30.  
    31.         }
    32.  
    33.        
    34.     }
    35.     public void OnTriggerExit(Collider other)
    36.     {
    37.         groundSpawner.SpawnTile();
    38.  
    39.         Destroy(gameObject, 10);
    40.  
    41.     }
    42.  
    43.  
    44.  
    45.  
    46. }
    47.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Line 29 you are instantiating stuff... you're not keeping the reference it gives you back.

    That reference returned by Instantiate() is the one you want to Destroy() later. Your line 39 is destroying the GameObject that this script is located on, not the thing you instantiated.

    It is the year 2021, you don't have to operate like this.

    I'm sorry you've had this issue. Please consider using proper industrial-grade source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally setting Unity up (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards
     
  3. Greywolf6

    Greywolf6

    Joined:
    Oct 20, 2020
    Posts:
    10
    I cant tell you how much I appreciate the information. I will put it to good use.
     
    Kurt-Dekker likes this.