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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Feedback Beginner here - Best Practice Question

Discussion in 'Scripting' started by illerhas, Oct 16, 2020.

  1. illerhas

    illerhas

    Joined:
    Sep 18, 2020
    Posts:
    10
    Hello I am following a tutorial on how to use Unity, doing a basic shmup. Where i am at he is saying to create a script for an object that gets instantiated, and the script's entire purpose is to destroy the clone. Is it better to break code down into multiple scripts, or just ad the couple extra lines in the script that instantiates the object?

    Here is the code that I used;
    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.tag == "Bullet")
    4.         {
    5.             Destroy(other.gameObject);
    6.             if (_player != null)
    7.             {
    8.                 _player.AddScore(5);
    9.             }
    10.             Destroy(gameObject, .2f);
    11.             GameObject newExplosion = Instantiate(_explosionPrefab, transform.position, Quaternion.identity);
    12.             Destroy(newExplosion, 2.38f);
    13.         }
    14.         if (other.tag == "Player")
    15.         {
    16.             if (_player != null)
    17.             {
    18.                 _player.Damage();
    19.             }
    20.             Destroy(gameObject, .2f);
    21.             GameObject newExplosion = Instantiate(_explosionPrefab, transform.position, Quaternion.identity);
    22.             Destroy(newExplosion, 2.38f);
    23.         }
    24.     }
    What he has done is to create script for the Explosion, and that script just destroys the object, So instead of Destroy(newExplosion, 2.38f); like i have in line, he wrote a script that just says:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         Destroy(this.gameObject, 2.38f);
    4.     }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'd say that creating an entire script which does nothing but destroy itself is just unnecessary overhead.
    It's literally just one line of code that can be applied anywhere. Might as well make separate scripts for other one-line functions also.

    Adding the
    Destroy
    call in the
    OnTriggerEnter2D
    method (or wherever else) is perfectly fine.
     
    illerhas likes this.
  3. illerhas

    illerhas

    Joined:
    Sep 18, 2020
    Posts:
    10
    Thank you!
    That was my thought process too, but since I am so new I wasn't entirely sure if one way or the other actually took more to process or if there was other reason i didn't think of as to why one way would be better than the other.