Search Unity

Two Scripts Different Function, consuming Same Gameobjects (merge or not?)

Discussion in 'Scripting' started by WirealTalha, Sep 11, 2019.

  1. WirealTalha


    Jul 19, 2018
    Hi, The situation is that i have two scripts both using same 50 Gameobjects. Script A is responsible for tweaking the alpha value of canvasgroup attached to the gamobjects in the update function and Script B is responsible for a certain "X" functionality.

    Now My question is Can I merge both scripts? So that I dont have to duplicate the references of all 50 Gameobjects.

    Which option is better?
    Merging the scripts?
    or keeping the scripts seperate?
  2. palex-nx


    Jul 23, 2018
    You should not. Keeping responsibilities separated is very important. If you don't you'll get a mess of code wich is impossible or to hard to fix/modify. Now to your concrete situation. You have 3, not two responsibilities to separate.
    First is tweaking alpha, second is X, and third is maintaining list of that objects. Create a list maintainer and reference it from both A and B scripts.
    Dameon_ likes this.